#!/usr/bin/env bash
#
#  Copyright (c) 2018 - 2019 by YaoLinuX team (http://yaolinux.org)
#  Copyright (c) 2014 - 2018 by NuTyX team (http://nutyx.org)
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
#  USA.
#
#

# gettext initialization
#
export TEXTDOMAIN='setup-nutyx'
export TEXTDOMAINDIR='/usr/share/locale'
. gettext.sh
. /lib/lsb/init-functions

#
# Basics functions
#
info() {
	echo -e  " ====> $1"
}
error() {
	info "${FAILURE}ERROR: ${NORMAL}$1" >&2
	cleanup
	exit 1

}
warning() {
	info "${WARNING}WARNING: ${NORMAL}$1"
}
msgbox () {
	dialog --msgbox "${1}" 0 0
}
is_true()
{
 [ "$1" = "1" ] || [ "$1" = "yes" ] || [ "$1" = "true" ]
}
_zoneinfo(){
	ln -sf ${1} ${2}
	$SETUP_LOG "setup-nutyx --config-lang" "ln -sv ${1} ${2}"
}
_msgfmt() {
	local L C
	L=`echo ${1}|cut -d "_" -f1`
	C=`echo ${1}|cut -d "_" -f2`
	echo "group ${L}" >> ${CARDS}
	localedef -i ${1} -f UTF-8 ${1}.UTF-8
	if [ -f /lib/services/setup-nutyx/setup-nutyx.${L} ]; then
		mkdir -p $TEXTDOMAINDIR/${L}/LC_MESSAGES
		msgfmt /lib/services/setup-nutyx/$TEXTDOMAIN.${L} \
		-o $TEXTDOMAINDIR/${L}/LC_MESSAGES/setup-nutyx.mo
		TEXTDOMAINFILE=$TEXTDOMAINDIR/${L}/LC_MESSAGES/$TEXTDOMAIN.mo
	fi
	echo "LANG=${1}.utf8" > ${LOCALE}
	echo "export LANG=${1}.utf8" > ${PROFILELOCALE}
	$SETUP_LOG "setup-nutyx --config-lang" "variable LANG=${1}.utf8 saved in ${LOCALE} and ${PROFILELOCALE}"
}
calc(){
        echo "scale=2;$@"|bc
}
#
# Main functions
#
interrupt() {
dialog --title " $(gettext "ERROR configuration") " \
	--msgbox " \n $(gettext "Please try again") " 7 32
exit 1
}
network_card_found() {
local NIC
NIC=`ls /sys/class/net`
if [  `echo $NIC|wc -w` -gt 1 ];then
	retval=""
	touch $TMP/result
	while [ true ]; do
		echo  -n "--backtitle \" $(gettext "Network card") \" \
		--title \" $(gettext "Select the card") \" \
		--radiolist \" $(gettext "Card to configure") \" 0 0 0 " > $TMP/list
		for i in $NIC
		do
			[ "$i" == "lo" ] && continue
			#checked if only one entry.
			if [  `echo $NIC|wc -w` -eq 2 ]; then
	  		echo -n \""$i"\"\  \"`/sbin/udevadm info /sys/class/net/$i|grep ID_MODEL_FROM_DATABASE|cut -d "=" -f2`\" " on "
			else
  			echo -n \""$i"\"\  \"`/sbin/udevadm info /sys/class/net/$i|grep ID_MODEL_FROM_DATABASE|cut -d "=" -f2`\" " off "
			fi
			done >> $TMP/list
			dialog --file $TMP/list 2>$TMP/result
			retval=$?
			result=$(cat $TMP/result)
			case $retval in
				0)
					if [ "$result" != "" ]; then
						break
					fi;;
				1|255)
					if [ "$result" != "" ]; then
						break
					fi;;
			esac
	done
	NETWORK_CARD=`cat $TMP/result|sed 's/"//g'`
	$SETUP_LOG "setup-nutyx --config-net" "Network card found: $NETWORK_CARD"
	rm -f $TMP/result
	rm -f $TMP/list
fi
}
check_group_exist()
{
	(getent group "${1}" > /dev/null) && return 1
	return 0
}
check_user_exist()
{
	(getent passwd "${1}" > /dev/null) && return 1
	return 0
}
check_user_valid()
{
	# Parameters:
	# $1: user name
	# $2: user description
	# Return value:
	# 0: user and description valid
	# 1: Invalid name
	# 2: name already exist
	# 3: Invalid description

	local desc_test name_test

	name_test=`echo -n "${1}" | sed 's@^[a-z][a-z0-9]*$@@g'`
	if [ "$name_test" != "" ]; then
		dialog --msgbox "$(gettext "The name you enter is invalid")" 5 60
		return 1
	fi
	check_user_exist ${1}
	if [ $? -ne 0 ]; then
		dialog --msgbox "$(gettext "The name you enter is already register on the system")" 6 60
		return 2
	fi
	desc_test=`echo "${2}" | sed 's@^[[:alnum:]! -.,~_@;%<>?]*$@@ig'`
	if [ "$desc_test" != "" ]; then
		dialog --msgbox "$(gettext "The description you enter is invalid")" 5 60
		return 3
	fi
	return 0
}
setup_password()
{
	local PASSWORD CONFIRM_PASSWORD RESULT

	PASSWORD=""
	CONFIRM_PASSWORD=""
	RESULT="false"

	while [ "$RESULT" == "false" ];
	do
		exec 3>&1
		PASSWORD=`dialog --title "  $(gettext "Password") " \
		--insecure "$@" \
		--passwordbox "  $(gettext "Enter a new password"):" 8 60  2>&1 1>&3`
		CONFIRM_PASSWORD=`dialog --title "  $(gettext "Password") " \
		--insecure "$@" \
		--passwordbox "$(gettext "Confirm the new password"):" 8 60 2>&1 1>&3`

		if [ "$PASSWORD" == "$CONFIRM_PASSWORD" ]; then
			RESULT="true"
		else
			dialog --msgbox "$(gettext "Passwords are differents, please try again")" 5 60
		fi
	done

	echo -e "$PASSWORD\n$CONFIRM_PASSWORD"|passwd "$NAME" 2> /dev/null
	$SETUP_LOG "setup-nutyx --create-user" "$NAME password changed"
}
setup_user()
{
	
	/usr/sbin/pwconv
	/usr/sbin/grpconv
	if [ -d /.home ]; then
		mv /.home /home
	else
		mkdir -p /home
	fi
	local RESULT
	USER_GROUPS=""
	DEFAULT_GROUPS=(users disk network floppy fuse video lp lpadmin netdev tty audio cdrom scanner adm vboxusers wheel)
	# Avoid spaces troubles :)
	OLD_IFS=$IFS
	IFS="|
"
	RESULT="user"
	while [ "$RESULT" == "user" ];
	do
		exec 3>&1
		RESULT=`dialog --title "  $(gettext "Add a user to the system")  " \
		--inputbox  "$(gettext "Name of the user"):" 8 60 user 2>&1 1>&3`
		RESULT="`echo "$RESULT"|sed "s/ //g"`"
		[ "$RESULT" == "" ] && RESULT="user"
	done
	NAME=$RESULT
	if [ "$NAME" == "root" ]; then
		rm -d /home
		setup_password
	else
		if [ "$RESULT" != "user" ] &&  [ "$RESULT" != "" ];then
			RESULT="Description"
			while [ "$RESULT" == "Description" ];
			do
				exec 3>&1
				RESULT=`dialog --title "  $(gettext "Add a description for the user")  " \
				--inputbox  "$(gettext "Name or Description of the user"):" 8 60 "Description" 2>&1 1>&3`
				[ "`echo "$RESULT"|sed "s/ //g"`" == "" ] && RESULT="Description"
				[ "$RESULT" == "" ] && RESULT="Description"
			done
			DESCRIPTION=$RESULT
		fi
		check_user_valid $NAME $DESCRIPTION
		ret=$?
		while [ $ret -ne 0 ]; do
			setup_user
		done
	
		for GROUP in ${DEFAULT_GROUPS[@]};
		do
			check_group_exist $GROUP
			if [ $? -eq 0 ]; then
				continue
			fi
			if [ -z "$USER_GROUPS" ]; then
				USER_GROUPS="$GROUP"
			else
				USER_GROUPS="$USER_GROUPS,$GROUP"
			fi
		done

		unset RESULT

		if [ -d /home/"${NAME}" ];then
			RESULT=`/usr/sbin/useradd -c "${DESCRIPTION}" -G "$USER_GROUPS" -M "${NAME}" 2>&1`
		else
			RESULT=`/usr/sbin/useradd -c "${DESCRIPTION}" -G "$USER_GROUPS" -m "${NAME}" 2>&1`
		fi
		if [ ! -z "$RESULT" ]; then
			dialog --msgbox "$RESULT" 6 120
			exec 3>&1
			dialog --yesno "$(gettext "Do you want to try again") ?" 5 40
			case $? in
				0)
					setup_user;;
				*)
					SETUP_CONFIG_USER="yes"
					rm -d /home;;
			esac
		else
			if [ -d /home/"${NAME}" ];then
				chown -R ${NAME}:${NAME} /home/${NAME}
				$SETUP_LOG "setup-nutyx --create-user" "chown -R ${NAME}:${NAME} /home/${NAME}"
				$SETUP_LOG "setup-nutyx --create-user" "${DESCRIPTION} -G $USER_GROUPS -M ${NAME}"
			else
				$SETUP_LOG "setup-nutyx --create-user" "${DESCRIPTION} -G $USER_GROUPS -m ${NAME}"
			fi
			setup_password
		fi
		SETUP_CONFIG_USER="no"
	fi
}
choose_available_ESSID() {
OLD_IFS=$IFS
IFS="
"
[ -z $"WIFI_NETWORK_CARD" ] && WIFI_NETWORK_CARD=`cat $TMP/result|sed 's/"//g'`

DEVTYPE=`/sbin/udevadm info /sys/class/net/$WIFI_NETWORK_CARD | grep DEVTYPE | cut -d"=" -f2`
if [ "$DEVTYPE" = "wlan" ]; then
	echo -n "--backtitle \" $(gettext "Networks") \" \
	--title \" $1 \" \
	--colors \
	--radiolist \" $(gettext "Choose the name of the wireless access point") (ESSID) \" 0 0 0 " > $TMP/networks

	NETWORKS=`iwlist $WIFI_NETWORK_CARD scanning 2>/dev/null | grep "ESSID:" | cut -d":" -f2 | grep -v "\\x00"`
	[ -z "$NETWORKS" ] && return
	for i in $NETWORKS; do
		[ $i == "\"\"" ] && continue
		echo -n "$i \" \" off"
	done >> $TMP/networks

	dialog --file $TMP/networks 2>$TMP/result
	retval=$?
	result="$(cat $TMP/result)"
	case $retval in
		0)
			if [ "$result" != "" ]; then
				ESSID=`cat $TMP/result`
				echo -n "--title \" $1 \" \
				--colors \
				--passwordbox \" $(gettext "Enter the password of") (PASSPHRASE): \Zb\Z4$ESSID\Zn \" 0 0" > $TMP/passwd
				dialog --insecure --file $TMP/passwd 2>$TMP/result
				retval=$?
				result="$(cat $TMP/result)"
				case $retval in
					0)
						if [ "$result" != "" ]; then
							KEY="$(cat $TMP/result)"
							WPA_FILE=/etc/wpa_supplicant.conf.${NETWORK_CARD}
							[ -f $WPA_FILE ] && mv $WPA_FILE ${WPA_FILE}.old
							wpa_passphrase "$ESSID" "$KEY" > $WPA_FILE
							chmod 600 ${WPA_FILE}
						fi ;;
					1|255)
						if [ "$result" != "" ]; then
							break
						fi ;;
				esac
				rm $TMP/result
			fi ;;
		1|255)
			if [ "$result" != "" ]; then
				break
			fi ;;
	esac
fi
IFS=$OLD_IFS
rm $TMP/result
rm $TMP/networks
rm $TMP/passwd
}
setup_wireless()
{
	local TITLE WPA_FILE returncode WPA_TOOL ESSID KEY
	WPA_TOOL="$(which wpa_passphrase)"
	IWCONFIG="$(which iwconfig)"

	if [ ! -z $IWCONFIG ];then
		WIFI_NETWORK_CARD="$($IWCONFIG 2>&1|grep IEEE|cut -d " " -f1|tail -1)"
	fi
	if [ -z ${WIFI_NETWORK_CARD} ];then
		network_card_found
		[ -z ${NETWORK_CARD} ] && return
		WIFI_NETWORK_CARD=${NETWORK_CARD}
	fi
	TITLE="$(gettext "Configuration of a wireless access point for") \Zb\Z1${WIFI_NETWORK_CARD}\Zn"

	ESSID=""
	KEY=""
	choose_available_ESSID "$TITLE"
	while [ "$ESSID" == "" ];
	do
		exec 3>&1
		ESSID=$(dialog  --title " $TITLE " \
		--colors \
		--inputbox "$(gettext "Choose the name of the wireless access point") (ESSID)" \
		0 0 ""	2>&1 1>&3)
		ESSID="$(echo "$ESSID"|sed "s/ //g")"
	done

	while [ "$KEY" == "" ];
	do
		exec 3>&1
		KEY=$(dialog --title " $TITLE " \
		--colors \
		--inputbox "$(gettext "Enter the password of") (PASSPHRASE): \Zb\Z4$ESSID\Zn" \
		0 0 ""  2>&1 1>&3)
		KEY="$(echo "$KEY"|sed "s/ //g")"
	done

	WPA_FILE="/etc/wpa_supplicant.conf.${WIFI_NETWORK_CARD}"
	rm -f $WPA_FILE

	[ -z $WPA_TOOL ] || $WPA_TOOL "$ESSID" "$KEY" > $WPA_FILE
	chmod 600 $WPA_FILE

	if [ -f $WPA_FILE ]; then
		dialog --clear --title " $TITLE " \
		--colors \
		--msgbox "$(gettext "File") \Zb\Z4$WPA_FILE\Zn $(gettext "successfully created")" 0 0
		$SETUP_LOG "setup-nutyx --config-wireless" "$ESSID configured"
	else
		dialog  --clear --title " $TITLE " \
		--colors \
		--msgbox "\Zb\Z1$(gettext "Cannot create") $WPA_FILE\Zn" 0 0
	fi
	SETUP_CONFIG_WIFI="no"
}
setup_net() 
{
network_card_found
NETWORK_DIR="/etc/systemd/network/"
retval=""
while [ "$retval" == "" ]; do
	dialog  \
	--title " $(gettext "Configuration of") ${NETWORK_CARD} " \
	--radiolist "$(gettext "Configuration mode")" \
	0 0 0 "$(gettext "Auto")" "$(gettext "IP address automatically set from DHCP server")" "on" \
	"$(gettext "Man")" "$(gettext "Manually specify parameters")" "off" 2> $TMP/result
	retval=$?
	result=$(cat $TMP/result)
	case $retval in
		0)
			if [ "$result" == "" ]; then
				retval=""
			fi;;
		1|255)
				exit;;
  esac
done
if [ "$(cat $TMP/result)" == "$(gettext "Auto")" ];then

	echo "[Match]
Name=${NETWORK_CARD}

[Network]
DHCP=ipv4

[DHCP]
UseDomains=true

" > ${NETWORK_DIR}${NETWORK_CARD}-dhcp.network
	$SETUP_LOG "setup-nutyx --config-net" "$NETWORK_CARD set to Auto DHCP"
else
	dialog \
	--title " $(gettext "Configuration of") ${NETWORK_CARD} " \
	--inputbox " $(gettext "Enter an IP address") : " 10 60 "192.168.1.5/24" 2> $TMP/IP

	if [ "$(cat $TMP/IP)" == "" ]; then
		interrupt
	else
		 IP=$(tail -n 1 $TMP/IP)
	fi
	BC_TEMP=$(echo $IP|cut -d . -f 1).$(echo $IP|cut -d . -f 2).$(echo $IP|cut -d . -f 3).255
	dialog --title " $(gettext "Configuration of") ${NETWORK_CARD} " \
	--inputbox " $(gettext "Enter a broadcast address"),\
	$(gettext "in most cases the current value can be used"): " 10 60 ${BC_TEMP} 2> $TMP/BC
	if [ "$(cat $TMP/BC)" == "" ]; then
		interrupt
	else
		BROADCAST=$(tail -n 1 $TMP/BC)
	fi
	GW_TEMP=$(echo $IP|cut -d . -f 1).$(echo $IP|cut -d . -f 2).$(echo $IP|cut -d . -f 3).1
	dialog \
	--title " $(gettext "Configuration of") ${NETWORK_CARD} " \
	--inputbox "$(gettext "Enter the gateway address"),\
	$(gettext "it is normally the address of your router access point"): " 10 60 ${GW_TEMP} 2> $TMP/GW
	GW=$(tail -n 1 $TMP/GW)
	dialog \
	--title " $(gettext "Configuration of") ${NETWORK_CARD} " \
	--inputbox "$(gettext "Enter the DNS address"),\
	$(gettext "it is normally the address of your router access point"): " 10 60 ${GW} 2> $TMP/DNS
	DNS=$(tail -n 1 $TMP/DNS)

	/sbin/ifconfig ${NETWORK_CARD} ${IP} broadcast ${BROADCAST} netmask ${PREFIX}

	if [ -n ${GW} ]; then
		/sbin/ip route add default via ${GW}
	fi
	echo "[Match]
Name=${NETWORK_CARD}

[Network]" > ${NETWORK_DIR}${NETWORK_CARD}-static.network

	$SETUP_LOG "setup-nutyx --config-net" "$NETWORK_CARD: SERVICE=ipv4-static"
	echo "Address=${IP}" >> ${NETWORK_DIR}${NETWORK_CARD}-static.network
	if [ -n ${GW} ]; then
		echo "Gateway=${GW}" >> ${NETWORK_DIR}${NETWORK_CARD}-static.network
		$SETUP_LOG "setup-nutyx --config-net" "$NETWORK_CARD: GATEWAY=${GW}"
	fi
	$SETUP_LOG "setup-nutyx --config-net" "$NETWORK_CARD: BROADCAST=${BROADCAST}"
	if [ -n "${DNS}" ]; then
		dialog \
		--title " $(gettext "DNS Search Suffix") " \
		--inputbox "$(gettext "Enter the domain name"),\
		$(gettext "this is only need if you are in a subdomain").\
		$(gettext "Most of the time it's not need"): " 10 60 2> $TMP/SUFFIX
		SUFFIX=$(tail -n 1 $TMP/SUFFIX)
		echo "DNS=${DNS}"  >> ${NETWORK_DIR}${NETWORK_CARD}-static.network
		$SETUP_LOG "setup-nutyx --config-net" "/etc/resolv.conf: nameserver ${DNS}"
		if [ -n "${SUFFIX}" ]; then
			echo "Domains=${SUFFIX}" >> ${NETWORK_DIR}${NETWORK_CARD}-static.network
			echo "search ${SUFFIX}" >> /etc/resolv.conf
			$SETUP_LOG "setup-nutyx --config-net" "/etc/resolv.conf: search ${SUFFIX}"
		fi
	fi
fi
dialog  --title " $(gettext "Name of your machine") " \
        --inputbox " $(gettext "Enter a name") : " 8 40 "yaolinux" 2> $TMP/NAME

        if [ "$(cat $TMP/NAME)" == "" ]; then
                interrupt
        else
                HOSTNAME=$(tail -n 1 $TMP/NAME)
                echo $HOSTNAME > /etc/hostname
                echo "# Begin /etc/hosts

127.0.0.1 localhost.localdomain localhost $HOSTNAME
# End /etc/hosts
" > /etc/hosts
        fi
[ ! -f /etc/hostname ] && echo "yaolinux" > /etc/hostname
do_start_network
SETUP_CONFIG_NETWORK="no"
}
setup_language()
{
/usr/bin/localedef -i fr_FR -f UTF-8 fr_FR.UTF-8
/usr/bin/setfont LatGrkCyr-8x16
while [ true ]; do
dialog   \
--no-ok \
--timeout 60 \
--no-cancel \
--title "" --menu "" 0 0 0 \
"United Arab Emirates" "Arabic" \
"Algeria" "Arabic" \
"Egypt" "Arabic" \
"India" "Arabic" \
"Iraq" "Arabic" \
"Jordan" "Arabic" \
"Kuwait" "Arabic" \
"Lebanon" "Arabic" \
"Libyan Arab Jamahiriya" "Arabic" \
"Moroco" "Arabic" \
"Oman" "Arabic" \
"Qatar" "Arabic" \
"Saudi Arabia" "Arabic" \
"Sudan" "Arabic" \
"South Sudan" "Arabic" \
"Syrian Arab Republic" "Arabic" \
"Tunisia" "Arabic" \
"Yemen" "Arabic" \
"Azerbaijan" "Azerbaycanca" \
"Cataluña" "Catala" \
"Česko" "čeština" \
"cymru" "Cymraeg" \
"Danmark" "Dansk" \
"Deutschland" "Deutsch" \
"Österreich" "Deutsch" \
"Schweiz" "Deutsch" \
"Luxemburg"  "Deutsch" \
"Belgien" "Deutsch" \
"Eesti" "Eesti" \
"Australia" "English" \
"Botswana" "English" \
"Canada" "English" \
"Denmark" "English" \
"United Kingdom" "English" \
"Hong Kong" "English" \
"Ireland" "English" \
"India " "English" \
"Indonesia" "English" \
"Nigeria" "English" \
"New Zealand" "English" \
"Philippines" "English" \
"Singapore" "English" \
"United States" "English" \
"South Africa" "English" \
"Zambia" "English" \
"Zimbabwe" "English" \
"Argentina" "Español" \
"Bolivia" "Español" \
"Chile" "Español" \
"Colombia" "Español" \
"Costa Rica" "Español" \
"Cuba" "Español" \
"Dominican Republic" "Español" \
"Ecuador" "Español" \
"España" "Español" \
"Guatemala" "Español" \
"Honduras" "Español" \
"Mexico" "Español" \
"Nicaragua" "Español" \
"Panama" "Español" \
"Peru" "Español" \
"Puerto Rico" "Español" \
"Paraguay" "Español" \
"El Salvador" "Español" \
"Estados Unidos" "Español" \
"Uruguay" "Español" \
"Venezuala" "Español" \
"Euskera" "Euskera" \
"France" "Français" \
"Suisse" "Français" \
"Belgique" "Français" \
"Luxembourg" "Français" \
"Quebec" "Français" \
"Galego" "Galego" \
"Ελληνική Δημοκρατία" "Ελληνικά" \
"Hrvatska" "Hrvatski" \
"Iceland" "Icelandic" \
"Ireland" "Irish" \
"Italia" "Italiano" \
"Svizzera" "Italiano" \
"latviešu" "latviešu" \
"lietuvių" "lietuvių" \
"kinyarwanda" "kinyarwanda" \
"Magyar" "Magyar" \
"Aruba" "Nederlands" \
"België" "Nederlands" \
"The Nederlands" "Nederlands" \
"Norge" "Norsk bokmål" \
"Poland" "Polski" \
"Россия" "русский" \
"Brazil" "Português" \
"Portugal" "Português" \
"română" "română" \
"shqip" "shqip" \
"slovenčina" "slovenčina" \
"Slovenščina" "Slovenščina" \
"Srbija" "Srpski" \
"Suomi" "Suomi" \
"Sverige" "Svenska" \
"Türkiye" "Türk" \
"Thaïland" "Thaï" \
"Ukraïna" "Ukraïna" \
"Viêt nam" "Viêt nam" \
2> $TMP/language.check

#timeout => exit
if [ $? = 255 ]; then
  exit 1
fi
if [ ! $? = 0 ]; then
  break
fi
REPLY=$(cat $TMP/language.check)
break
done
sed -i "/group/d" /etc/cards.conf
case ${REPLY} in
 "United Arab Emirates")
	_msgfmt ar_AE
	_zoneinfo ${ZONEINFO}Asia/Dubai ${LOCALTIME};;
 "Algeria")
	_msgfmt ar_DZ
	_zoneinfo ${ZONEINFO}Africa/Addis_Ababa ${LOCALTIME};;
 "Egypt")
	_msgfmt ar_EG
	_zoneinfo ${ZONEINFO}Egypt ${LOCALTIME};;
 "India")
	_msgfmt ar_IN
	_zoneinfo ${ZONEINFO}Asia/Kolkata ${LOCALTIME};;
 "Iraq")
	_msgfmt ar_IQ
	_zoneinfo ${ZONEINFO}Asia/Baghdad ${LOCALTIME};;
 "Jordan")
	_msgfmt ar_IQ
	_zoneinfo ${ZONEINFO}Asia/Amman ${LOCALTIME};;
 "Kuwait")
	_msgfmt ar_KW
	_zoneinfo ${ZONEINFO}Asia/Kuwait ${LOCALTIME};;
 "Lebanon")
	_msgfmt ar_LB
	_zoneinfo ${ZONEINFO}Asia/Beirut ${LOCALTIME};;
 "Libyan Arab Jamahiriya")
	_msgfmt ar_LY
	_zoneinfo ${ZONEINFO}Africa/Tripoli ${LOCALTIME};;
 "Moroco")
	_msgfmt ar_MA
	_zoneinfo ${ZONEINFO}Africa/Casablanca ${LOCALTIME};;
 "Oman")
	_msgfmt ar_OM
	_zoneinfo ${ZONEINFO}Asia/Muscat ${LOCALTIME};;
 "Qatar")
	_msgfmt ar_QA
	_zoneinfo ${ZONEINFO}Asia/Qatar ${LOCALTIME};;
 "Saudi Arabia")
	_msgfmt ar_SA
	_zoneinfo ${ZONEINFO}Asia/Riyadh ${LOCALTIME};;
 "Sudan")
	_msgfmt ar_SD
	_zoneinfo ${ZONEINFO}Africa/Khartoum ${LOCALTIME};;
 "South Sudan")
	_msgfmt ar_SS
	_zoneinfo ${ZONEINFO}Africa/Khartoum ${LOCALTIME};;
 "Syrian Arab Republic")
	_msgfmt ar_SY
	_zoneinfo ${ZONEINFO}Asia/Damascus ${LOCALTIME};;
 "Tunisia")
	_msgfmt ar_TN
	_zoneinfo ${ZONEINFO}Africa/Tunis ${LOCALTIME};;
 "Yemen")
	_msgfmt ar_YE
	_zoneinfo ${ZONEINFO}Asia/Aden ${LOCALTIME};;
 "Azerbaijan")
	_msgfmt az_AZ
	_zoneinfo ${ZONEINFO}Asia/Baku;;
 "Cataluña")
	_msgfmt ca_ES
	_zoneinfo ${ZONEINFO}Europe/Madrid ${LOCALTIME};;
 "Cestina")
	_msgfmt cs_CZ
	_zoneinfo ${ZONEINFO}Europe/Prague ${LOCALTIME};;
 "Cymraeg")
	_msgfmt cy_GB
	_zoneinfo ${ZONEINFO}Europe/London ${LOCALTIME};;
 "Danmark")
	_msgfmt da_DK
	_zoneinfo ${ZONEINFO}Europe/Copenhagen ${LOCALTIME};;
 "Deutschland")
	_msgfmt de_DE
	_zoneinfo ${ZONEINFO}Europe/Berlin ${LOCALTIME};;
 "Österreich")
	_msgfmt de_AT
	_zoneinfo ${ZONEINFO}Europe/Vienna ${LOCALTIME};;
 "Schweiz")
	_msgfmt de_CH
	_zoneinfo ${ZONEINFO}Europe/Zurich ${LOCALTIME};;
 "Luxemburg")
	_msgfmt de_LU
	_zoneinfo ${ZONEINFO}Europe/Luxembourg ${LOCALTIME};;
 "Belgien")
	_msgfmt de_BE
	_zoneinfo ${ZONEINFO}Europe/Brussels ${LOCALTIME};;
 "Eesti")
	_msgfmt et_EE
	_zoneinfo ${ZONEINFO}Europe/Tallinn ${LOCALTIME};;
 "Australia")
	_msgfmt en_AU
	_zoneinfo ${ZONEINFO}Australia/Sydney ${LOCALTIME};;
 "Botswana")
	_msgfmt en_BW
	_zoneinfo ${ZONEINFO}Africa/Gaborone ${LOCALTIME};;
 "Canada")
	_msgfmt en_CA
	_zoneinfo ${ZONEINFO}Canada/Central ${LOCALTIME};;
 "Denmark")
	_msgfmt en_DK
	_zoneinfo ${ZONEINFO}Europe/Copenhagen ${LOCALTIME};;
 "United Kingdom")
	_msgfmt en_GB
	_zoneinfo ${ZONEINFO}Europe/London ${LOCALTIME};;
 "Hong Kong")
	_msgfmt en_HK
	_zoneinfo ${ZONEINFO}Asia/Hong_Kong ${LOCALTIME};;
 "Ireland")
	_msgfmt en_IE
	_zoneinfo ${ZONEINFO}Europe/Dublin ${LOCALTIME};;
 "India ")
	_msgfmt en_IN
	_zoneinfo ${ZONEINFO}Indian/Maldives ${LOCALTIME};;
 "Nigeria")
	_msgfmt en_NG
	_zoneinfo ${ZONEINFO}Africa/Lagos ${LOCALTIME};;
 "New Zealand")
	_msgfmt en_NZ
	_zoneinfo ${ZONEINFO}Pacific/Auckland ${LOCALTIME};;
 "Indonesia")
	_msgfmt en_ID
	_zoneinfo ${ZONEINFO}Asia/Jakarta ${LOCALTIME};;
 "Philippines")
	_msgfmt en_PH
	_zoneinfo ${ZONEINFO}Asia/Manila ${LOCALTIME};;
 "Singapore")
	_msgfmt en_SG
	_zoneinfo ${ZONEINFO}Asia/Singapore ${LOCALTIME};;
 "United States")
	_msgfmt en_US
	_zoneinfo ${ZONEINFO}America/New_York ${LOCALTIME};;
 "South Africa")
	_msgfmt en_ZA
	_zoneinfo ${ZONEINFO}Africa/Johannesburg ${LOCALTIME};;
 "Zambia")
	_msgfmt en_ZM
	_zoneinfo ${ZONEINFO}Africa/Lusaka ${LOCALTIME};;
 "Zimbabwe")
	_msgfmt en_ZW
	_zoneinfo ${ZONEINFO}Africa/Harare ${LOCALTIME};;
 "Argentina")
	_msgfmt es_AR
	_zoneinfo ${ZONEINFO}America/Buenos_Aires ${LOCALTIME};;
 "Bolivia")
	_msgfmt es_BO
	_zoneinfo ${ZONEINFO}America/La_paz ${LOCALTIME};;
 "Chile")
	_msgfmt es_CL
	_zoneinfo ${ZONEINFO}America/Santiago ${LOCALTIME};;
 "Colombia")
	_msgfmt es_CO
	_zoneinfo ${ZONEINFO}America/Bogota ${LOCALTIME};;
 "Costa Ricar")
	_msgfmt es_CR
	_zoneinfo ${ZONEINFO}America/Costa_Rica ${LOCALTIME};;
 "Cuba")
	_msgfmt es_CU
	_zoneinfo ${ZONEINFO}America/Havana ${LOCALTIME};;
 "Dominican Republic")
	_msgfmt es_DO
	_zoneinfo ${ZONEINFO}America/Santo_Domingo ${LOCALTIME};;
 "Ecuador")
	_msgfmt es_EC
	_zoneinfo ${ZONEINFO}America/Guayaquil ${LOCALTIME};;
 "España")
	_msgfmt es_ES
	_zoneinfo ${ZONEINFO}Europe/Madrid ${LOCALTIME};;
 "Guatemala")
	_msgfmt es_GT
	_zoneinfo ${ZONEINFO}America/Guatemala ${LOCALTIME};;
 "Honduras")
	_msgfmt es_HN
	_zoneinfo ${ZONEINFO}America/Tegucigalpa ${LOCALTIME};;
 "Mexico")
	_msgfmt es_MX
	_zoneinfo ${ZONEINFO}America/Mexico_City ${LOCALTIME};;
 "Nicaragua")
	_msgfmt es_NI
	_zoneinfo ${ZONEINFO}America/Managua ${LOCALTIME};;
 "Panama")
	_msgfmt es_PA
	_zoneinfo ${ZONEINFO}America/Panama ${LOCALTIME};;
 "Peru")
	_msgfmt es_PE
	_zoneinfo ${ZONEINFO}America/Lima ${LOCALTIME};;
 "Puerto Rico")
	_msgfmt es_PR
	_zoneinfo ${ZONEINFO}America/Argentina/San_Juan ${LOCALTIME};;
 "Paraguay")
	_msgfmt es_PY
	_zoneinfo ${ZONEINFO}America/Asuncion ${LOCALTIME};;
 "El Salvador")
	_msgfmt es_SV
	_zoneinfo ${ZONEINFO}America/El_Salvador ${LOCALTIME};;
 "Estados Unidos")
	_msgfmt es_US
	_zoneinfo ${ZONEINFO}America/New_York ${LOCALTIME};;
 "Uruguay")
	_msgfmt es_UY
	_zoneinfo ${ZONEINFO}America/Montevideo ${LOCALTIME};;
 "Venezuala")
	_msgfmt es_VE
	_zoneinfo ${ZONEINFO}America/Caracas ${LOCALTIME};;
 "Euskera")
	_msgfmt es_ES
	_zoneinfo ${ZONEINFO}Europe/Madrid;;
 "France")
	_msgfmt fr_FR
	_zoneinfo ${ZONEINFO}Europe/Paris ${LOCALTIME};;
 "Belgique")
	_msgfmt fr_BE
	_zoneinfo ${ZONEINFO}Europe/Brussels ${LOCALTIME};;
 "Suisse")
	_msgfmt fr_CH
	_zoneinfo ${ZONEINFO}Europe/Zurich ${LOCALTIME};;
 "Luxembourg")
	_msgfmt fr_LU
	_zoneinfo ${ZONEINFO}Europe/Luxembourg ${LOCALTIME};;
 "Quebec")
	_msgfmt fr_CA
	_zoneinfo ${ZONEINFO}America/Montreal ${LOCALTIME};;
 "Ελληνικά")
	_msgfmt el_GR
	_zoneinfo ${ZONEINFO}Europe/Athens ${LOCALTIME};;
 "Galego")
	_msgfmt gl_ES
	_zoneinfo ${ZONEINFO}Europe/Madrid ${LOCALTIME};;
 "Hrvatska")
	_msgfmt hr_HR
	_zoneinfo ${ZONEINFO}Europe/Zagreb ${LOCALTIME};;
 "Iceland")
	_msgfmt is_IS
	_zoneinfo ${ZONEINFO}Atlantic/Reykjavik ${LOCALTIME};;
 "Ireland")
	_msgfmt ga_IE
	_zoneinfo ${ZONEINFO}Europe/Dublin ${LOCALTIME};;
 "Italia")
	_msgfmt it_IT
	_zoneinfo ${ZONEINFO}Europe/Rome ${LOCALTIME};;
 "Svizzera")
	_msgfmt it_CH
	_zoneinfo ${ZONEINFO}Europe/Zurich ${LOCALTIME};;
 "kinyarwanda")
	_msgfmt rw_RW
	_zoneinfo ${ZONEINFO}/Africa/Kigali ${LOCALTIME};;
 "latviešu")
	_msgfmt lv_LV;;
 "lietuvių")
	_msgfmt lt_LT;;
 "Magyar")
	_msgfmt hu_HU;;
 "Aruba")
	_msgfmt nl_AW
	_zoneinfo ${ZONEINFO}America/Aruba ${LOCALTIME};;
 "België")
	_msgfmt nl_BE
	_zoneinfo ${ZONEINFO}Europe/Brussels ${LOCALTIME};;
 "The Nederlands")
	_msgfmt nl_NL
	_zoneinfo ${ZONEINFO}Europe/Amsterdam ${LOCALTIME};;
 "Norge")
	_msgfmt nb_NO
  _msgfmt nn_NO
  	echo "LANG=nb_NO.UTF-8" > ${LOCALE}
	echo "export LANG=nb_NO.UTF-8" > ${PROFILELOCALE}
	_zoneinfo ${ZONEINFO}Europe/Oslo ${LOCALTIME};;
 "Polski")
	_msgfmt pl_PL
	echo "LANG=pl_PL.UTF-8" > ${LOCALE}
	echo "export LANG=pl_PL.UTF-8" > ${PROFILELOCALE}
	_zoneinfo ${ZONEINFO}Europe/Warsaw ${LOCALTIME};;
 "Россия")
  _msgfmt ru_RU
  	echo "LANG=ru_RU.UTF-8" > ${LOCALE}
	echo "export LANG=ru_RU.UTF-8" > ${PROFILELOCALE}
	_zoneinfo ${ZONEINFO}Europe/Moscow ${LOCALTIME};;
 "Portugal")
	_msgfmt pt_PT
	echo "LANG=pt_PT.UTF-8" > ${LOCALE}
	echo "export LANG=pt_PT.UTF-8" > ${PROFILELOCALE}
	_zoneinfo ${ZONEINFO}Europe/Lisbon ${LOCALTIME};;
 "Brasil")
	_msgfmt pt_BR
	_zoneinfo ${ZONEINFO}America/Sao_Paulo ${LOCALTIME};;
 "română")
	_msgfmt ro_RO
	_zoneinfo ${ZONEINFO}Europe/Bucharest ${LOCALTIME};;
 "shqip")
	_msgfmt sq_MK
	_zoneinfo ${ZONEINFO}Europe/Tirane ${LOCALTIME};;
 "slovenčina")
	_msgfmt sk_SK
	_zoneinfo ${ZONEINFO}Europe/Bratislava ${LOCALTIME};;
 "Slovenščina")
	_msgfmt sl_SI
	_zoneinfo ${ZONEINFO}Europe/Ljubljana ${LOCALTIME};;
 "Srbija")
	_msgfmt sr_RS
	_zoneinfo ${ZONEINFO}Europe/Belgrade ${LOCALTIME};;
 "Suomi")
	_msgfmt fi_FI
	_zoneinfo ${ZONEINFO}Europe/Helsinki ${LOCALTIME};;
 "Sverige")
	_msgfmt sv_SE
	_zoneinfo ${ZONEINFO}Europe/Stockholm ${LOCALTIME};;
 "Türkiye")
	_msgfmt tr_TR
	_zoneinfo ${ZONEINFO}Europe/Istanbul ${LOCALTIME};;
 "Thaïland")
	_msgfmt th_TH
	_zoneinfo ${ZONEINFO}Asia/Bangkok ${LOCALTIME};;
 "Ukraïna")
	_msgfmt uk_UA
	_zoneinfo ${ZONEINFO}Europe/Kiev ${LOCALTIME};;
 "Viêt nam")
	_msgfmt vi_VN
	_zoneinfo ${ZONEINFO}Asia/Ho_Chi_Minh ${LOCALTIME};;
esac
[ -f ${LOCALE} ] &&  . ${LOCALE}
SETUP_CONFIG_LANGUAGE="no"
}
setup_keymap ()
{
	cat > ${CONSOLE} << "EOF"
FONT="LatGrkCyr-8x16"
UNICODE="1"
EOF
	cat > ${XORGKB} << "EOF"
Section "InputClass"
  Identifier "Generic Keyboard"
  MatchIsKeyboard "yes"
EOF
while [ true ]; do
dialog  \
--title " $(gettext "Keyboard Layout") " \
--backtitle " $(gettext "Settings") " --menu " $(gettext "Choose available"): " 0 0 0 \
"fr-pc" "AZERTY" \
"fr-latin9" "AZERTY" \
"be-latin1" "AZERTY" \
"fr_CH-latin1" "QWERTZ" \
"de_CH-latin1" "QWERTZ" \
"de-latin1-nodeadkeys" "QWERTZ" \
"de" "QWERTZ" \
"cf" "QWERTY" \
"us" "QWERTY" \
"de-latin1" "QWERTY" \
"azerty" "AZERTY" \
"it-ibm" "QWERTY" \
"it" "QWERTY" \
"it2" "QWERTY" \
"sv-latin1" "QWERTY" \
"nl" "QWERTY" \
"nl2" "QWERTY" \
"no-latin1" "QWERTY" \
"no" "QWERTY" \
"ANSI-dvorak" "AOEUID" \
"dvorak-l" "AOEUID" \
"dvorak-r" "AOEUID" \
"dvorak" "AOEUID" \
"fi-latin1" "QWERTYUIOPÅ" \
"fi-latin9" "QWERTYUIOPÅ" \
"croat" "QWERTZ" \
"ru-cp1251" "QWERTY" \
"ru-ms" "" \
"ru-yawerty" "" \
"ru" "" \
"ru1" "" \
"ru2" "" \
"ru3" "" \
"ru4" "" \
"ru_win" "" \
"applkey" "" \
"backspace" "" \
"bg-cp1251" "" \
"bg-cp855" "" \
"bg_bds-cp1251" "" \
"bg_bds-utf8" "" \
"bg_pho-cp1251" "" \
"bg_pho-utf8" "" \
"br-abnt" "" \
"br-abnt2" "" \
"br-latin1-abnt2" "" \
"br-latin1-us" "" \
"by" "" \
"ctrl" "" \
"cz-cp1250" "" \
"cz-lat2-prog" "" \
"cz-lat2" "" \
"cz-us-qwertz" "" \
"cz" "" \
"dk-latin1" "" \
"dk" "" \
"defkeymap" "" \
"defkeymap_V1.0" "" \
"emacs" "" \
"emacs2" "" \
"es-cp850" "" \
"es" "" \
"et-nodeadkeys" "" \
"et" "" \
"euro" "" \
"euro1" "" \
"euro2" "" \
"fi" "" \
"gr-pc" "" \
"gr" "" \
"hu" "" \
"hu101" "" \
"il-heb" "" \
"il-phonetic" "" \
"il" "" \
"is-latin1-us" "" \
"is-latin1" "" \
"jp106" "" \
"keypad" "" \
"la-latin1" "" \
"list" "" \
"lt.baltic" "" \
"lt" "" \
"lt.l4" "" \
"mk-cp1251" "" \
"mk-utf" "" \
"mk" "" \
"mk0" "" \
"pc110" "" \
"pl" "" \
"pl2" "" \
"pt-latin1" "" \
"pt-latin9" "" \
"ro_win" "" \
"se-fi-ir209" "" \
"se-fi-lat6" "" \
"se-ir209" "" \
"se-lat6" "" \
"sg-latin1-lk450" "" \
"sg-latin1" "" \
"sg" "" \
"sk-prog-qwerty" "" \
"sk-prog-qwertz" "" \
"sk-qwerty" "" \
"sk-qwertz" "" \
"slovene" "" \
"speakup-jfw" "" \
"speakupmap" "" \
"sr-cy" "" \
"tr_f-latin5" "" \
"tr_q-latin5" "" \
"tralt" "" \
"trf" "" \
"trq" "" \
"ua-utf-ws" "" \
"ua-utf" "" \
"ua-ws" "" \
"ua" "" \
"uk" "" \
"unicode" "" \
"us-acentos" "" \
"windowkeys" "" 2> $TMP/locale.check

if [ ! $? = 0 ]; then
  break
fi
REPLY=`cat $TMP/locale.check`
rm -f $TMP/locale.check
break
done
sed -i "/KEYMAP/d" ${CONSOLE}
echo "KEYMAP=${REPLY}.map" >> ${CONSOLE}
$SETUP_LOG "setup-nutyx --config-keymap" "KEYMAP=${REPLY}.map saved in ${CONSOLE}"
loadkeys ${REPLY}.map > /dev/null 2>&1
case $REPLY in
	us)
		echo '  Option "XkbLayout" "us"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="us"' > ${WAYLKB};;
	be-latin1|wangbe*)
		echo '  Option "XkbLayout" "be"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="be"' > ${WAYLKB};;
	bg*) echo '  Option XkbLayout "bg"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="bg"' > ${WAYLKB};;
	cf) echo '  Option "XkbLayout" "cf"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="cf"' > ${WAYLKB};;
	cz*) echo '  Option "XkbLayout" "cz"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="cz"' > ${WAYLKB};;
	fr|fr-*|azerty)
		echo '  Option "XkbLayout" "fr"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="fr"' > ${WAYLKB};;
	fr_CH*) echo '  Option "XkbLayout" "ch(fr)"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="ch(fr)"' > ${WAYLKB};;
	de_CH*) echo '  Option "XkbLayout" "ch"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="ch"' > ${WAYLKB};;
	de-*) echo '  Option "XkbLayout" "de"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="de"' > ${WAYLKB};;
	dk|dk-latin1)
		echo '  Option "XkbLayout" "dk"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="dk"' > ${WAYLKB};;
	es|es-*) echo '  Option "XkbLayout" "es"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="es"' > ${WAYLKB};;
	hu|hu101)
		echo '  Option "XkbLayout" "hu"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="hu"' > ${WAYLKB};;
	gr) echo '  Option "XkbLayout" "gr"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="gr"' > ${WAYLKB};;
	nl|nl2) echo '  Option "XkbLayout" "nl"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="nl"' > ${WAYLKB};;
	it*) echo '  Option "XkbLayout" "it"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="it"' > ${WAYLKB};;
	lu*) echo '  Option "XkbLayout" "lu"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="lu"' > ${WAYLKB};;
	pt*) echo '  Option "XkbLayout" "pt"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="pt"' > ${WAYLKB};;
	no*) echo '  Option "XkbLayout" "no"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="no"' > ${WAYLKB};;
	se*) echo '  Option "XkbLayout" "sv"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="sv"' > ${WAYLKB};;
	fi*) echo '  Option "XkbLayout" "fi"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="fi"' > ${WAYLKB};;
	ga*) echo '  Option "XkbLayout" "ga"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="ga"' > ${WAYLKB};;
	el*) echo '  Option "XkbLayout" "el"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="el"' > ${WAYLKB};;
	pl*) echo '  Option "XkbLayout" "pl"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="pl"' > ${WAYLKB};;
	ro_*) echo '  Option "XkbLayout" "ro"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="ro"' > ${WAYLKB};;
	ru*) echo '  Option "XkbLayout" "ru"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="ru"' > ${WAYLKB};;
	sr*) echo '  Option "XkbLayout" "sr"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="sr"' > ${WAYLKB};;
	slo*) echo '  Option "XkbLayout" "sk"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="sk"' > ${WAYLKB};;
	cro*) echo '  Option "XkbLayout" "hr"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="hr"' > ${WAYLKB};;
	tr*) echo '  Option "XkbLayout" "tr"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="tr"' > ${WAYLKB};;
	uk) echo ' Option "XkbLayout" "uk"' >> ${XORGKB}
		echo 'export XKB_DEFAULT_LAYOUT="uk"' > ${WAYLKB};;
esac
echo "EndSection" >> ${XORGKB}
$SETUP_LOG "setup-nutyx --config-keymap" "${XORGKB} adjusted"
SETUP_CONFIG_KEYBOARD="no"
}

setup_clock() {
	local TABDATE
	HWCLOCK=$(LANG=C.utf8 hwclock -r)
	dialog --title "   $(gettext "Coordinated Universal Time or Local Time ?")  " \
	--yesno "$(gettext "The hardware clock is set to"):\n\n $HWCLOCK. \n\n$(gettext "Do you want to use this time as Coordinated Universal Time ?") \n\
$(gettext "so that the summer/winter time is changed automatically" ) " 0 0
	retval=$?
	case $retval in
		0)
			UTC=1;;
		1)
			UTC=0;;
	esac

	echo "0.0 0.0" > /etc/adjtime
  if [ "$UTC" == "0" ];then
		echo "0
LOCAL" >> /etc/adjtime
	else
		echo "1
REMOTE" >> /etc/adjtime
	fi

	case "${UTC}" in
		1)
			CLOCKPARAMS="${CLOCKPARAMS} --utc";;
		0)
			CLOCKPARAMS="${CLOCKPARAMS} --localtime";;
	esac
	
	hwclock --hctosys ${CLOCKPARAMS} >/dev/null

	exec 3>&1
	DESC=" $(gettext "Use the arrows keys to change the values") "
	USERDATE=`dialog --title " $(gettext "Please enter the date") " \
	--calendar "$DESC" 0 0 2>&1 1>&3`
	code=$?
	exec 3>&-
	case $code in
		0)
			TABDATE=($(echo $USERDATE|sed "s|/| |g"))
			GOODDATE="${TABDATE[2]}${TABDATE[1]}${TABDATE[0]} "
			;;
		1)
			SETUP_CONFIG_CLOCK="no"
			break;;
	esac
	exec 3>&1
	USERTIME=`dialog --title " $(gettext "Please enter the time") " \
	--timebox  "$DESC" 0 0 2>&1 1>&3`
	code=$?
	exec 3>&-
	case $code in
		0)
			echo "$GOODDATE $USERTIME" > $TMP/date;;
		1)
			break;;
	esac

	date -s "$GOODDATE $USERTIME"

	hwclock --systohc ${CLOCKPARAMS}
	$SETUP_LOG "setup-nutyx --config-clock" "$(gettext "Date and time settings"): $(date -s "$GOODDATE $USERTIME")"

	SETUP_CONFIG_CLOCK="no"
}
select_disk() {
	local retval result
	retval=""
	result=""
	while [ "$retval" == "" ]; do
		dialog --title "   ${1}    " \
		--menu \
		"  $(gettext "Choose the disk you want to use for this operation")  " \
		0 0 0  \
		`for DISK in $DISK_LIST; do  lsblk -p -n -d -o NAME,SIZE $DISK; done` \
		2> $TMP/disk
		retval=$?
		result=`cat $TMP/disk`
	done
	case $retval in
		0)
			if [ "$result" == "" ]; then
				retval=""
			fi;;
		1|255)
			break;;
	esac
	DISK=`cat $TMP/disk|sed 's/"//g'|sed 's/ //g'`
	DISK=`echo $DISK|sed  "s/ //"`
	if [ -z $DISK ]; then
		DISK="no"
	fi
}
select_partition() {
	local retval result
	retval=""
	result=""
	while [ "$retval"  == "" ]; do
		dialog --cancel-label "$(gettext "Main Menu")" \
		--title " ${1} " \
		--menu " $(gettext "Choose the partition you want to use for this operation") " \
		0 0 0 \
		`for PARTITION in ${PARTITION_LIST}; do lsblk -d -p -n -o NAME,SIZE $PARTITION; done`  2>$TMP/part
		retval=$?
		result=`cat $TMP/part`
		case $retval in
			0)
				if [ "$result" == "" ]; then
					retval=""
				fi;;
			1|255)
				break;;
		esac
	done
	PARTITION=`cat $TMP/part|sed 's/"//g'|sed 's/ //g'`
	if [ -z $PARTITION ];then
		dialog --no-cancel \
		--title " $(gettext "No partition available") " \
		--msgbox " $(gettext "Please create/modify yours partitions") " 0 0
		PARTITION="no"
	fi
}
check_available_disk() {
	unset DISK_LIST
	local DISK_FULL_LIST
	DISK_FULL_LIST=$(lsblk -p -n -d -o NAME -e 1,3,7,11)
	for i in $DISK_FULL_LIST; do
		[ "$(lsblk -p -n -d -o LABEL $i)" == "yaolinuxcd" ] && continue
		[ "$(echo $i|grep fd[0-9]*)" == "$i" ] && continue
		DISK_LIST="$DISK_LIST $i"
	done
	if [ -z "$DISK_LIST" ]; then
		dialog --no-cancel \
		--title " $(gettext "No disk available") " \
		--msgbox " $(gettext "Please stop the PC and install a harddisk") " 6 60
	else
		# Only one disk ?
		if [ $(echo $DISK_LIST|wc -w) -eq 1 ]; then
			DISK=$DISK_LIST
			DISK=$(echo $DISK|sed  "s/ //")
			echo "$DISK_LIST" > $TMP/disk
		fi
		CHECK_HD_OK="yes"
	fi
	echo "DISK_LIST=$DISK_LIST" > /dev/tty2
}
check_available_partition() {
	unset PARTITION_LIST
	local PARTITION_FULL_LIST
	if [ "$CHECK_HD_OK" == "no" ]; then
		check_available_disk
		 [ "$CHECK_HD_OK" == "no" ] && return
	fi
	PARTITION_LIST=$(for i in $DISK_LIST; do lsblk -p -l -n -o NAME $i| tail -n +2; done)
	if [ -z "$PARTITION_LIST" ];then
		dialog --no-cancel \
		--title " $(gettext "No partition available") " \
		--msgbox " $(gettext "Please create/modify yours partitions") " 0 0
		CHECK_PART_OK="no"
		setup_partition
	else
		echo "PARTITION_LIST=$PARTITION_LIST" > /dev/tty2
		[ -d "$EFIVARS" ] && check_esp
		check_swap
		for i in ${SWAP_LIST}
		do
			echo "SWAP_PARTITION=$i" > /dev/tty2
			PARTITION_LIST=$(echo ${PARTITION_LIST}| sed "s@$i@@")
		done
		echo "PARTITION_LIST=$PARTITION_LIST" > /dev/tty2
		if [ ! -z "$EFI_SYS_PART" ]; then
			echo "EFI_SYS_PART=$EFI_SYS_PART" > /dev/tty2
			PARTITION_LIST=$(echo ${PARTITION_LIST}| sed "s@$EFI_SYS_PART@@")
		fi
		echo "PARTITION_LIST=$PARTITION_LIST" > /dev/tty2
		if [ ! -z "$PARTITION_LIST" ]; then
			# Only one partition ?
			if [ $(echo $PARTITION_LIST|wc -w) -eq 1 ];then
				# Only one disk
				if [ $(echo $DISK_LIST|wc -w) -eq 1 ]; then
					PARTITION=$(echo $PARTITION_LIST|sed 's/ //g')
					echo "PARTITION=$PARTITION" > /dev/tty2
					CHECK_SINGLE_PARTITION="yes"
					echo "CHECK_SINGLE_PARTITION=$CHECK_SINGLE_PARTITION"  > /dev/tty2
				fi
			fi
			CHECK_PART_OK="yes"
		fi
		echo "PARTITION_LIST=$PARTITION_LIST" > /dev/tty2
	fi
}
setup_help() {
	dialog --no-cancel \
	--title "   $(gettext "How to do it")  " \
	--colors --msgbox \
"$(gettext "This is a very basic tool, do not expect too much"). \
$(gettext "It will ask you on which partition you want to install YaoLinuX"). \
$(gettext "If not yet created/formatted, it will prompt you to do so").\n\
\n\n$(gettext "YaoLinuX goes on") \
\Zu\Zb\Z1$(gettext "only ONE partition")\Zn\n\n\
$(gettext "ONE exception, GRUB can be part of it, installed on a separate partition or not installed at all"). \n\n\
$(gettext "As YaoLinuX users, we recommand you to install GRUB on a separate partition"):\n\n\
$(gettext "To do so"): \
$(gettext "Install GRUB first, then YaoLinuX") \n\n\
$(gettext "Have fun") :) \n\n\
$(gettext "Thank you for installing YaoLinuX") \
" 0 0
}
setup_help_efi() {
	dialog --no-cancel \
	--title "   $(gettext "How to do it")  " \
	--colors --msgbox \
"$(gettext "This is a very basic tool, do not expect too much"). \
$(gettext "It will ask you on which partition you want to install YaoLinuX"). \
$(gettext "If not yet created/formatted, it will prompt you to do so").\n\
\n$(gettext "YaoLinuX goes on") \
\Zu\Zb\Z1$(gettext "only ONE partition")\Zn\n\n\
$(gettext "Make sure you reserve the first partition for ESP"): \n\n\
$(gettext "To do so"): \
$(gettext "At partitioning, declare the first partition as EFI System") \n\n\
$(gettext "Have fun") :) \n\n\
$(gettext "Thank you for installing YaoLinuX") \
" 0 0

}	
setup_partition() {
	unset DISK_LIST
	unset PARTITION_LIST
	unset DISK
	local retval result
	check_available_disk
	[ "$CHECK_HD_OK" == "no" ] && return
	retval=""
	if [ -z $DISK ]; then
		select_disk "$(gettext "Create all your partitions")"
		[ "$DISK" == "no" ] && return
	fi
	dialog --title " $(gettext "Choose your favorite tool") " \
	--menu "$(gettext "Partitioning of the disk"): $result" 15 55 2 \
	"cfdisk" "$(gettext "Basic partitioning tool")" \
	"fdisk" "$(gettext "Advanced partitioning tool")" 2>$TMP/choix
	retval=$?
	choice=$(cat $TMP/choix)
	rm -f $TMP/choix
	case $retval in
		0|2)
			case $choice in
				cfdisk)
					cfdisk $DISK;;
				fdisk)
					fdisk $DISK;;
			esac
			;;
		1|2544)
			break;;
	esac
	check_available_partition
}
setup_format() {
	local retval retval2 result
	retval=""
	retval2=""
	if [ "$CHECK_HD_OK" == "no" ]; then
		check_available_disk
		[ "$CHECK_HD_OK" == "no" ] && return
	fi
	check_available_partition
	[ "$CHECK_PART_OK" == "no" ] && return
	if [ -z "$PARTITION" ]; then
		select_partition "$(gettext "Format a partition")"
		[ "$PARTITION" == "no" ] && return
	fi
	dialog --title " $(gettext "Available file systems") " \
	--colors --menu " $(gettext "Choose the file system you want to use for the partition") \
\Zb\Z1$PARTITION\Zn" 0 0 0 \
		"btrfs" "$(gettext "New promising file system") ($(gettext "recommended"))" \
		"ext4" "$(gettext "Next generation of Ext3 file system") ($(gettext "recommended"))" \
		"ext3" "$(gettext "Journaling version of Ext2 file system")"  \
		"ext2" "$(gettext "Standard file system Ext2")" \
		"reiserfs" "$(gettext "Very stable file system but not maintain anymore")" \
		"xfs" "$(gettext "High performance journaling file system create by SGI")" \
    "jfs" "$(gettext "Journaling file system create by IBM")" \
		2> $TMP/result
		result=$(cat $TMP/result)
		if [ ! "$result" == "" ]; then
			dialog --title " $(gettext "Launch the format process ?") " \
			--colors --yesno "$(gettext "The partition") \Zb\Z1$PARTITION\Zn \
$(gettext "will be formatted in") \Zb\Z1$result\Zn.\n\n\
$(gettext "Are you sure you want to continue ?")" 7 60
				retval2=$?
		fi
		case $retval2 in
			0)
				case $result in
					ext2)
						mkfs.ext2 -F $PARTITION > /dev/tty2 || error "mkfs.ext2 $PARTITION"
						$SETUP_LOG "setup-nutyx" "mkfs.ext2 -F $PARTITION"
						;;
					ext3)
						mkfs.ext3 -F $PARTITION > /dev/tty2 || error "mkfs.ext3 $PARTITION"
						$SETUP_LOG "setup-nutyx" "mkfs.ext3 -F $PARTITION"
						;;
					ext4)
						mkfs.ext4 -F $PARTITION > /dev/tty2 || error "mkfs.ext4 $PARTITION"
						$SETUP_LOG "setup-nutyx" "mkfs.ext4 -F $PARTITION"
						;;
					reiserfs)
						mkreiserfs -q $PARTITION > /dev/tty2 || error "mkreiserfs -q $PARTITION"
						$SETUP_LOG "setup-nutyx" "mkreiserfs -q $PARTITION"
						;;
					btrfs)
						mkfs.btrfs -f $PARTITION > /dev/tty2 || error "mkfs.btrfs -f $PARTITION"
						$SETUP_LOG "setup-nutyx" "mkfs.btrfs -f $PARTITION"
						;;
					xfs)
						mkfs.xfs -f -m crc=0 $PARTITION > /dev/tty2 || error "mkfs.xfs -f $PARTITION"
						$SETUP_LOG "setup-nutyx" "mkfs.xfs -f -m crc=0 $PARTITION"
						;;
					jfs)
						mkfs.jfs -f $PARTITION > /dev/tty2 || error "mkfs.jfs -f $PARTITION"
						$SETUP_LOG "setup-nutyx" "mkfs.jfs -f $PARTITION"
						;;
				esac;;
			1|255)
				break;;
		esac
		FORMAT=$(print_file_system $PARTITION)
}
erase_yaolinux() {
	local DIR REAL
	cd ${1}/boot
	if [ -d ${2}/home ];then
		mv ${2}/{home,.home}
	fi
	MAX=`ls *squashfs|wc -l`
	N=0
	(for i in *squashfs
	do
		DIR=`echo $i|cut -d "." -f1`
		[ "$DIR" == "root" ] && continue
		[ ! -z $DIR ] && rm -rf ${2}/${DIR}
		REAL=`calc $N/$MAX *100`
		PCT=${REAL%.*}
		echo "$PCT"
		N=$((N+1))
	done) |
  dialog --title "$(gettext "Erase") YaoLinuX" --gauge "$(gettext "Please wait") ..." 6 50
  $SETUP_LOG "setup-nutyx" "Erase YaoLinuX on ${PARTITION} done"
}
install_yaolinux() {
	local DIR REAL FILE
	setup_swap

	[ -d "$EFIVARS" ] && setup_esp

	cd ${1}/boot
	MAX=$(ls *squashfs|wc -l)
	N=0
	(for i in *squashfs
	do
		DIR=$(echo $i|cut -d "." -f1)
		unsquashfs -f -n -d ${2}/${DIR} ${i} >/dev/null
		REAL=$(calc $N/$MAX *100)
		PCT=${REAL%.*}
		echo "$PCT"
		N=$((N+1))
	done

	chmod 750 ${2}/root
	mkdir ${2}/boot 2>/dev/null
	echo "$PARTITION   /    $FORMAT  defaults     0    0" >> /etc/fstab
	if [ -d "$EFIVARS" ]; then
		mkdir -p ${2}/boot/efi
		echo "efivarfs    /sys/firmware/efi/efivars    efivarfs    rw,nosuid,nodev,noexec,noatime 0 0" >> /etc/fstab
	else
		mkdir -p ${2}/boot
	fi

	for FILE in etc/profile.d/i18n.sh \
	etc/systemd/network/* \
	etc/hostname \
	etc/adjtime \
	etc/cards.conf \
	etc/fstab \
	etc/network \
	etc/resolv.conf \
	etc/mdadm.conf \
	$TEXTDOMAINFILE
	do
		if [ -f /$FILE ] || [ -d /$FILE ] || [ -L /$FILE ];then
			cp -a /$FILE  ${2}/$FILE
		fi
	done
	if [ -f /etc/locale/locale-archive ]; then
		rm -f ${2}/usr/lib/locale
		cp -a /etc/locale ${2}/usr/lib/
	fi
	cp -a ${1}/boot/kernel* ${2}/boot/
	for i in /lib/modules/*
	do
		j=$(basename $i)
		ke=-$(echo $j|cut -d "-" -f3)
		[ ${#ke} == 1 ] && ke=""
		ln -sf kernel-$j ${2}/boot/kernel$ke
	done
	if  [ -f ${1}/boot/grub/splash.png ]; then
		mkdir -p ${2}/boot/grub/
		cp ${1}/boot/grub/splash.png ${2}/boot/grub/
	fi
	if [ -f ${1}/boot/grub/fonts/unicode.pf2 ]; then
		mkdir -p ${2}/boot/grub/fonts
		cp ${1}/boot/grub/fonts/unicode.pf2 ${2}/boot/grub/fonts
	fi
	for DIR in proc sys dev srv mnt tmp
	do
		[ ! -d ${2}/$DIR ] &&	mkdir ${2}/$DIR
	done
	[ -a ${2}/dev/console ] || mknod -m 640 ${2}/dev/console c 5 1
	[ -a ${2}/dev/null ] || mknod -m 664 ${2}/dev/null    c 1 3
	chmod 1777 ${2}/tmp
	echo "100") |
	dialog --title "$(gettext "Install YaoLinuX")" --gauge "$(gettext "Please wait") ..." 6 50
	$SETUP_LOG "setup-nutyx" "Install YaoLinuX on ${PARTITION} done"
	if [ -f /var/log/install.log ]; then
		cp -a /var/log/install.log  ${2}/var/log/install.log
	fi
}
setup_erase() {
	local MF retval
	retval=""
	if [ "$CHECK_HD_OK" == "no" ]; then
		check_available_disk
		[ "$CHECK_HD_OK" == "no" ] && return
	fi
	if [ "$CHECK_PART_OK" == "no" ]; then
		check_available_partition
		[ "$CHECK_PART_OK" == "no" ] && return
	fi
	echo "PARTITION=$PARTITION" > /dev/tty2
	echo "CHECK_SINGLE_PARTITION=$CHECK_SINGLE_PARTITION" > /dev/tty2
	[ "$CHECK_SINGLE_PARTITION" == "no" ] && unset PARTITION
	if [ -z $PARTITION ]; then
		select_partition "$(gettext "Erase") YaoLinuX"
		[ "$PARTITION" == "no" ] && return
	fi
	echo "PARTITION=$PARTITION" > /dev/tty2
	SETUP_INSTALL="no"
	MF=/mnt/hd
	mkdir -p $MF
	mount $PARTITION $MF || error "$(gettext "mounting") $PARTITION"
	if [ -d /media/cdrom/boot ]; then
		erase_yaolinux "/media/cdrom" "$MF"
	else
		erase_yaolinux "/" "$MF"
	fi
	umount $PARTITION
}
setup_install() {
	local MF retval
	retval=""
  if [ "$CHECK_HD_OK" == "no" ]; then
    check_available_disk
    [ "$CHECK_HD_OK" == "no" ] && return
  fi
  if [ "$CHECK_PART_OK" == "no" ]; then
    check_available_partition
    [ "$CHECK_PART_OK" == "no" ] && return
  fi
	echo "PARTITION=$PARTITION" > /dev/tty2
	echo "CHECK_SINGLE_PARTITION=$CHECK_SINGLE_PARTITION" > /dev/tty2
	[ "$CHECK_SINGLE_PARTITION" == "no" ] && unset PARTITION
	if [ -z $PARTITION ]; then
		select_partition "$(gettext "Install YaoLinuX")"
		[ "$PARTITION" == "no" ] && return
	fi
	echo "PARTITION=$PARTITION" > /dev/tty2
	FORMAT=`print_file_system $PARTITION`
	echo "FORMAT=$FORMAT" > /dev/tty2
	if [ "$FORMAT" == "no" ]; then
		setup_format
		[ "$FORMAT" == "no" ] && return
	fi
	SETUP_INSTALL="no"
	MF=/mnt/hd
	mkdir -p $MF
	mount $PARTITION $MF || error "$(gettext "mounting") $PARTITION"
	if [ -d /media/cdrom/boot ]; then
		# Installing from ISO
		install_yaolinux "/media/cdrom/" "$MF"
	else
		# Installing from Maintenance this means
		# TODO the initrd has to be rebuild at configuration time
		install_yaolinux "/" "$MF"
	fi
	umount $PARTITION
	SETUP_INSTALL="yes"
	echo "$PARTITION" > /root/boot
	echo "$PARTITION" > $TMP/boot
	if [ ! -d "$EFIVARS" ];then
		dialog --title "   $(gettext "Configuration of the boot of the computer") (GRUB)   " \
		--yesno "$(gettext "Configure the boot of the PC") ? " 0 0
		retval=$?
		case $retval in
			0)
				setup_grub;;
		esac
	else
		FORMAT="no"
		[ ! -z $EFI_SYS_PART ] && FORMAT=`print_file_system $EFI_SYS_PART`
		if [ "$FORMAT" == "vfat" ];then
			dialog --title " $(gettext "Choose your favorite Boot Manager") " \
				--menu "$(gettext "Grub, Efibootmgr or nothing"): $result" 15 60 3 \
			        "Grub" "$(gettext "Advanced Linux Kernel boot manager")" \
				"Nothing" "$(gettext "To use the one used on other distro")" 2>$TMP/boot_choice
		        retval=$?
		        boot_choice=`cat $TMP/boot_choice`
		        case $retval in
		                0|2)
				        case $boot_choice in
						Grub)
				                        setup_grub_efi "$MF";;
						Nothing)
							continue;;
					esac
				;;
				1|2544)
				break
				;;
			esac
		else
			dialog --title "   Oups !!!  " \
			--msgbox "    $(gettext "This installation won't boot again ...")" 6 60
		fi
	fi
}
MBR_ok() {
	local retval
	retval=""
	dialog --title "   $(gettext "Everything OK, do you want to modify") $GRUB_CONFIG_FILE ?   " \
	--colors \
	--yesno " $(gettext "A new") $GRUB_CONFIG_FILE $(gettext "has been created"), \
$(gettext "it has been adapted to launch the Maintenance system of YaoLinuX"), \
$(gettext "if you choose") $(gettext "Yes"), \
$(gettext "an simplify VI editor will allow you to modify it").\n\n \
	\Zb\Z4i\Zn  $(gettext "Enter the edit mode") \n\
	\Zb\Z4Esc\Zn $(gettext "Exit the edit mode") \n\
	\Zb\Z4:q!\Zn $(gettext "Cancel the modifications and exit VI")\n\
	\Zb\Z4:x\Zn  $(gettext "Save and exit VI")\n" 0 0
	retval=$?
	case $retval in
		0|2)
			setup_keymap
			vi $GRUB_CONFIG_DIR/$GRUB_CONFIG_FILE;;
	esac
}

setup_grub_efi() {
        local GRUB_CONFIG_FILE GRUB_CONFIG_DIR GRUB_DESTINATION_DIR GRUB_LOG_FILE
        GRUB_CONFIG_FILE="grub.cfg"
        GRUB_CONFIG_DIR=""
        GRUB_DESTINATION_DIR=""
        GRUB_LOG_FILE=$TMP/grub.log
        if [ "$CHECK_HD_OK" == "no" ]; then
                check_available_disk
                [ "$CHECK_HD_OK" == "no" ] && return
        fi
        if [ "$CHECK_PART_OK" == "no" ]; then
                check_available_partition
                [ "$CHECK_PART_OK" == "no" ] && return
        fi
        if [ -z $DISK ]; then
                select_disk "$(gettext "Configure the boot of the PC")"
                [ "$DISK" == "no" ] && return
        fi
        if [ "$SETUP_INSTALL" != "yes" ]; then
                [ "$CHECK_SINGLE_PARTITION" == "no" ] && unset PARTITION
        fi
        if [ -z $PARTITION ]; then
                select_partition "$(gettext "Configure the boot of the PC")"
                [ "$PARTITION" == "no" ] && return
        fi
        FORMAT=`print_file_system $PARTITION`
        if [ "$FORMAT" == "no" ]; then
                setup_format
                [ "$FORMAT" == "no" ] && return
        fi
        # We saved the current MBR if no yet saved
#       dd if=$DISK of=$TMP/MBR bs=512 count=1 2>/dev/null
        # Copy all the necessary files
        mkdir $TMP/boot_partition
        mount $PARTITION $TMP/boot_partition
        echo "$PARTITION" > $TMP/boot_part
        if [ -d $TMP/boot_partition/boot ]; then
                # YaoLinuX is installed means we install GRUB after install
                GRUB_DESTINATION_DIR="boot/"
        fi
        GRUB_CONFIG_DIR="$TMP/boot_partition/${GRUB_DESTINATION_DIR}grub"
        mkdir $GRUB_CONFIG_DIR 2> /dev/null

	local SPNR OPTIONS
        OPTIONS=$(cat /proc/cmdline|sed "s@BOOT_IMAGE=/boot/kernel\ @@")
        SPNR=$(echo $PARTITION|sed "s@^/dev/[a-z]*@@")
        mount -t efivarfs efivarfs /sys/firmware/efi/efivars
        mkdir -p $TMP/boot_partition/boot/efi
        mount "$EFI_SYS_PART" $TMP/boot_partition/boot/efi
        mkdir -p $TMP/boot_partition/boot/efi/EFI
        rm -f $TMP/boot_partition/boot/efi/EFI/BOOT/$SPNR/* 2>/dev/null

#       if [ -f $GRUB_CONFIG_DIR/MBR.original ]; then
#               dialog --title " !!! Oups !!!! " \
#               --msgbox " $(gettext "You already have a copy of the original MBR, I will not make a backup of it") " \
#               6 50
#       else
#               cp $TMP/MBR $GRUB_CONFIG_DIR/MBR.original
#               dialog --title " $(gettext "Good to know") " \
#               --msgbox " $(gettext "A copy of the original MBR is saved in the folder /boot/grub of your YaoLinuX. It will then be possible to restore it if need") " 10 60
#       fi
        if [ ! -f $GRUB_CONFIG_DIR/$GRUB_CONFIG_FILE ]; then
                cat > $GRUB_CONFIG_DIR/$GRUB_CONFIG_FILE << "EOF"
# Begin grub.cfg
# By default boot the first menu entry.
set default=0
# Allow 5 seconds before booting the default.
set timeout=5

insmod all_video
if loadfont /boot/grub/fonts/unicode.pf2; then
 set gfxmode=800x600
 insmod efi_gop
 insmod efi_uga
 insmod video_bochs
 insmod video_cirrus
 insmod gfxterm
 insmod png
 terminal_output gfxterm
fi

if background_image /boot/grub/splash.png; then
  set color_normal=light-gray/black
  set color_highlight=white/black
else
  set color_normal=cyan/blue
  set color_highlight=white/blue
fi

EOF
        fi
        # Writing of NEW MBR
        BOOT_FS="$(print_file_system $PARTITION)"
        LANG=C /sbin/grub-install --recheck --force --boot-directory=$TMP/boot_partition/${GRUB_DESTINATION_DIR} --target=x86_64-efi ${DISK} > ${GRUB_LOG_FILE} 2>&1
        if ! (grep finished ${GRUB_LOG_FILE} > /dev/null); then
                echo "/sbin/grub-install --recheck --force --boot-directory=$TMP/boot_partition/${GRUB_DESTINATION_DIR} --target=x86_64-efi [`echo $DISK`]" >> ${GRUB_LOG_FILE}
                mount >> ${GRUB_LOG_FILE}
                echo "DISK=${DISK}" >> ${GRUB_LOG_FILE}
                echo "GRUB_DESTINATION_DIR=${GRUB_DESTINATION_DIR}" >> ${GRUB_LOG_FILE}
                echo "GRUB_CONFIG_DIR=${GRUB_CONFIG_DIR}" >> ${GRUB_LOG_FILE}
                echo "YaoLinuX_PART=${YaoLinuX_PART}" >> ${GRUB_LOG_FILE}
                echo "BOOT_FS=${BOOT_FS}" >> ${GRUB_LOG_FILE}
                echo "YaoLinuX_FS=${YaoLinuX_FS}" >> ${GRUB_LOG_FILE}
                echo "PATH=$PATH" >> ${GRUB_LOG_FILE}
                env >> ${GRUB_LOG_FILE}
                # Restore MBR
                dd if=$TMP/MBR of=$DISK bs=512 count=1 > 2/dev/null
                dialog --title " $(gettext "Something went wrong probably the file system is not supported") " \
                --msgbox "$(cat ${GRUB_LOG_FILE})" 30 70
		umount $TMP/boot_partition/boot/efi
                umount $TMP/boot_partition
                umount /mnt/hd 2>/dev/null
                error "Sorry, I cannot install GRUB" # die now
        fi
        if [ -f $TMP/boot ]; then
                #
                # The $TMP/boot file contains the YaoLinuX partition
                #
                YaoLinuX_PART="$(cat $TMP/boot)"
                BOOT_FS="$(print_file_system $PARTITION)"
                YaoLinuX_FS="$(print_file_system $YaoLinuX_PART)"
                echo "menuentry \"YaoLinuX LTS ${SETUP_ARCH} ${YaoLinuX_FS} on ${YaoLinuX_PART}\" {" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
                echo " linux /${GRUB_DESTINATION_DIR}kernel-lts root=${YaoLinuX_PART} ro quiet verbose=no" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
                echo "#  initrd /${GRUB_DESTINATION_DIR}initrd-lts" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
                echo "}" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
                echo " " >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
                echo "menuentry \"YaoLinuX LTS Verbose ${SETUP_ARCH} ${YaoLinuX_FS} on ${YaoLinuX_PART}\" {" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
                echo "  linux /${GRUB_DESTINATION_DIR}kernel-lts root=${YaoLinuX_PART} ro " >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
                echo "#  initrd /${GRUB_DESTINATION_DIR}initrd-lts" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
                echo "}" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
                echo " "  >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
                echo "menuentry \"YaoLinuX ${SETUP_ARCH} ${YaoLinuX_FS} on ${YaoLinuX_PART}\" {" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
                echo " linux /${GRUB_DESTINATION_DIR}kernel root=${YaoLinuX_PART} ro quiet verbose=no" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
                echo "#  initrd /${GRUB_DESTINATION_DIR}initrd" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
                echo "}" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
                echo " " >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
                echo "menuentry \"YaoLinuX Verbose ${SETUP_ARCH} ${YaoLinuX_FS} on ${YaoLinuX_PART}\" {" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
                echo "  linux /${GRUB_DESTINATION_DIR}kernel root=${YaoLinuX_PART} ro " >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
                echo "#  initrd /${GRUB_DESTINATION_DIR}initrd" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
                echo "}" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}

        else
                #
                # YaoLinuX is not yet installed
                #
                cat >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}  << "EOF"
#################################################################
#                                                               #
#               Example of a menuentry:                         #
#                                                               #
#   The YaoLinuX distribution to boot from is located:          #
#      on the SECOND harddisk ( HDD 2 ) AND                     #
#      the second harddisk has THREE partitions.                #
#      YaoLinuX IS installed on the third partition.            #
#                                                               #
# NOTE:                                                         #
# -----                                                         #
#                                                               #
#  ==>>>    Harddisk start AT ZERO                              #
#  ==>>>    Partition start AT ONE                              #
#                                                               #
# We need to inform GRUB that we use the kernel on              #
# the Partition Number 3 of Harddisk Number 2                   #
#                                                               #
#  ======>  set root=(hd1,3)                                    #
#                                                               #
# And we need to inform the kernel to look at the right place:  #
#                                                               #
#  ======>  root=/dev/sdb3                                      #
#                                                               #
#           Below the menuentry of this example:                #
#       ( adjust to your needs and remove the '#' )             #
#                                                               #
#################################################################
#
#menuentry  "Harddisk number 2 and partition number 3" {
#   set root=(hd1,3)
#   linux /boot/kernel-lts root=/dev/sdb3 ro quiet verbose=no
#}
#
# The same one for debug purpose:
#
#menuentry  "Harddisk number 2 and partition number 3 verbose mode" {
#   set root=(hd1,3)
#   linux /boot/kernel-lts root=/dev/sdb3 ro
#}
#
# An other one with initrd and rootdelay (to use with USB stick for ex)
#
#menuentry  "Harddisk number 2 and partition number 1 with initrd" {
#   set root=(hd1,1)
#   linux /boot/kernel-lts root=/dev/sdb1 ro rootdelay=5
#   initrd /boot/initrd-lts
#}
#
EOF
                if [ ! -f $TMP/boot_partition/${GRUB_DESTINATION_DIR}initrd ]; then
                        cp /media/cdrom/boot/initrd \
                        $TMP/boot_partition/${GRUB_DESTINATION_DIR}initrd 2> /dev/null
                        cp /media/cdrom/boot/kernel \
                        $TMP/boot_partition/${GRUB_DESTINATION_DIR}kernel 2> /dev/null
                fi
                MBR_ok
        fi
	umount "$EFI_SYS_PART"
        umount $PARTITION
}

setup_grub() {
	local GRUB_CONFIG_FILE GRUB_CONFIG_DIR GRUB_DESTINATION_DIR GRUB_LOG_FILE
	GRUB_CONFIG_FILE="grub.cfg"
	GRUB_CONFIG_DIR=""
	GRUB_DESTINATION_DIR=""
	GRUB_LOG_FILE=$TMP/grub.log
	if [ "$CHECK_HD_OK" == "no" ]; then
		check_available_disk
		[ "$CHECK_HD_OK" == "no" ] && return
	fi
	if [ "$CHECK_PART_OK" == "no" ]; then
		check_available_partition
		[ "$CHECK_PART_OK" == "no" ] && return
	fi
	if [ -z $DISK ]; then
		select_disk "$(gettext "Configure the boot of the PC")"
		[ "$DISK" == "no" ] && return
	fi
	if [ "$SETUP_INSTALL" != "yes" ]; then
		[ "$CHECK_SINGLE_PARTITION" == "no" ] && unset PARTITION
	fi
	if [ -z $PARTITION ]; then
		select_partition "$(gettext "Configure the boot of the PC")"
		[ "$PARTITION" == "no" ] && return
	fi
	FORMAT=$(print_file_system $PARTITION)
	if [ "$FORMAT" == "no" ]; then
		setup_format
		[ "$FORMAT" == "no" ] && return
	fi
	# We saved the current MBR if no yet saved
#	dd if=$DISK of=$TMP/MBR bs=512 count=1 2>/dev/null
	# Copy all the necessary files
	mkdir $TMP/boot_partition
	mount $PARTITION $TMP/boot_partition
	echo "$PARTITION" > $TMP/boot_part
	if [ -d $TMP/boot_partition/boot ]; then
		# YaoLinuX is installed means we install GRUB after install
		GRUB_DESTINATION_DIR="boot/"
	fi
	GRUB_CONFIG_DIR="$TMP/boot_partition/${GRUB_DESTINATION_DIR}grub"
	mkdir $GRUB_CONFIG_DIR 2> /dev/null
#	if [ -f $GRUB_CONFIG_DIR/MBR.original ]; then
#		dialog --title " !!! Oups !!!! " \
#		--msgbox " $(gettext "You already have a copy of the original MBR, I will not make a backup of it") " \
#		6 50
#	else
#		cp $TMP/MBR $GRUB_CONFIG_DIR/MBR.original
#		dialog --title " $(gettext "Good to know") " \
#		--msgbox " $(gettext "A copy of the original MBR is saved in the folder /boot/grub of your YaoLinuX. It will then be possible to restore it if need") " 10 60
#	fi
	if [ ! -f $GRUB_CONFIG_DIR/$GRUB_CONFIG_FILE ]; then
		cat > $GRUB_CONFIG_DIR/$GRUB_CONFIG_FILE << "EOF"
# Begin grub.cfg
# By default boot the first menu entry.
set default=0
# Allow 5 seconds before booting the default.
set timeout=5

insmod all_video
if loadfont /boot/grub/fonts/unicode.pf2; then
 set gfxmode=800x600
 insmod efi_gop
 insmod efi_uga
 insmod video_bochs
 insmod video_cirrus
 insmod gfxterm
 insmod png
 terminal_output gfxterm
fi

if background_image /boot/grub/splash.png; then
  set color_normal=light-gray/black
  set color_highlight=white/black
else
  set color_normal=cyan/blue
  set color_highlight=white/blue
fi

EOF
	fi
	# Writing of NEW MBR
	BOOT_FS="$(print_file_system $PARTITION)"
	LANG=C /sbin/grub-install --recheck --force --boot-directory=$TMP/boot_partition/${GRUB_DESTINATION_DIR} ${DISK} > ${GRUB_LOG_FILE} 2>&1
	if ! (grep finished ${GRUB_LOG_FILE} > /dev/null); then
		echo "/sbin/grub-install --recheck --force --boot-directory=$TMP/boot_partition/${GRUB_DESTINATION_DIR} [`echo $DISK`]" >> ${GRUB_LOG_FILE}
		mount >> ${GRUB_LOG_FILE}
		echo "DISK=${DISK}" >> ${GRUB_LOG_FILE}
echo "GRUB_DESTINATION_DIR=${GRUB_DESTINATION_DIR}" >> ${GRUB_LOG_FILE}
echo "GRUB_CONFIG_DIR=${GRUB_CONFIG_DIR}" >> ${GRUB_LOG_FILE}
echo "YaoLinuX_PART=${YaoLinuX_PART}" >> ${GRUB_LOG_FILE}
echo "BOOT_FS=${BOOT_FS}" >> ${GRUB_LOG_FILE}
echo "YaoLinuX_FS=${YaoLinuX_FS}" >> ${GRUB_LOG_FILE}
echo "PATH=$PATH" >> ${GRUB_LOG_FILE}
		env >> ${GRUB_LOG_FILE}
		# Restore MBR
		dd if=$TMP/MBR of=$DISK bs=512 count=1 > 2/dev/null
		dialog --title " $(gettext "Something went wrong probably the file system is not supported") " \
		--msgbox "`cat ${GRUB_LOG_FILE}`" 30 70
		umount $TMP/boot_partition
		umount /mnt/hd 2>/dev/null
		error "Sorry, I cannot install GRUB" # die now
	fi
	if [ -f $TMP/boot ]; then
		#
		# The $TMP/boot file contains the YaoLinuX partition
		#
		YaoLinuX_PART="`cat $TMP/boot`"
		BOOT_FS="`print_file_system $PARTITION`"
		YaoLinuX_FS="`print_file_system $YaoLinuX_PART`"
		echo "menuentry \"YaoLinuX LTS ${SETUP_ARCH} ${YaoLinuX_FS} on ${YaoLinuX_PART}\" {" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
		echo " linux /${GRUB_DESTINATION_DIR}kernel-lts root=${YaoLinuX_PART} ro quiet verbose=no" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
		echo "#  initrd /${GRUB_DESTINATION_DIR}initrd-lts" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
		echo "}" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
		echo " " >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
		echo "menuentry \"YaoLinuX LTS Verbose ${SETUP_ARCH} ${YaoLinuX_FS} on ${YaoLinuX_PART}\" {" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
		echo "  linux /${GRUB_DESTINATION_DIR}kernel-lts root=${YaoLinuX_PART} ro " >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
		echo "#  initrd /${GRUB_DESTINATION_DIR}initrd-lts" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
		echo "}" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
		echo " "  >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
		echo "menuentry \"YaoLinuX ${SETUP_ARCH} ${YaoLinuX_FS} on ${YaoLinuX_PART}\" {" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
		echo " linux /${GRUB_DESTINATION_DIR}kernel root=${YaoLinuX_PART} ro quiet verbose=no" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
		echo "#  initrd /${GRUB_DESTINATION_DIR}initrd" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
		echo "}" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
		echo " " >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
		echo "menuentry \"YaoLinuX Verbose ${SETUP_ARCH} ${YaoLinuX_FS} on ${YaoLinuX_PART}\" {" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
		echo "  linux /${GRUB_DESTINATION_DIR}kernel root=${YaoLinuX_PART} ro " >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
		echo "#  initrd /${GRUB_DESTINATION_DIR}initrd" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}
		echo "}" >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}

	else
		#
		# YaoLinuX is not yet installed
		#
		cat >> ${GRUB_CONFIG_DIR}/${GRUB_CONFIG_FILE}  << "EOF"
#################################################################
#                                                               #
#               Example of a menuentry:                         #
#                                                               #
#   The YaoLinuX distribution to boot from is located:          #
#      on the SECOND harddisk ( HDD 2 ) AND                     #
#      the second harddisk has THREE partitions.                #
#      YaoLinuX IS installed on the third partition.            #
#                                                               #
# NOTE:                                                         #
# -----                                                         #
#                                                               #
#  ==>>>    Harddisk start AT ZERO                              #
#  ==>>>    Partition start AT ONE                              #
#                                                               #
# We need to inform GRUB that we use the kernel on              #
# the Partition Number 3 of Harddisk Number 2                   #
#                                                               #
#  ======>  set root=(hd1,3)                                    #
#                                                               #
# And we need to inform the kernel to look at the right place:  #
#                                                               #
#  ======>  root=/dev/sdb3                                      #
#                                                               #
#           Below the menuentry of this example:                #
#       ( adjust to your needs and remove the '#' )             #
#                                                               #
#################################################################
#
#menuentry  "Harddisk number 2 and partition number 3" {
#   set root=(hd1,3)
#   linux /boot/kernel-lts root=/dev/sdb3 ro quiet verbose=no
#}
#
# The same one for debug purpose:
#
#menuentry  "Harddisk number 2 and partition number 3 verbose mode" {
#   set root=(hd1,3)
#   linux /boot/kernel-lts root=/dev/sdb3 ro
#}
#
# An other one with initrd and rootdelay (to use with USB stick for ex)
#
#menuentry  "Harddisk number 2 and partition number 1 with initrd" {
#   set root=(hd1,1)
#   linux /boot/kernel-lts root=/dev/sdb1 ro rootdelay=5
#   initrd /boot/initrd-lts
#}
#
EOF
		if [ ! -f $TMP/boot_partition/${GRUB_DESTINATION_DIR}initrd ]; then
			cp /media/cdrom/boot/initrd \
			$TMP/boot_partition/${GRUB_DESTINATION_DIR}initrd 2> /dev/null
			cp /media/cdrom/boot/kernel \
			$TMP/boot_partition/${GRUB_DESTINATION_DIR}kernel 2> /dev/null
		fi
		MBR_ok
	fi
	umount $PARTITION
}
setup_reboot() {
	cd /
	if [ -d /media/cdrom/${SETUP_ARCH} ]; then
	 umount /media/cdrom > /dev/null 2>&1
	fi
	for i in /mnt/*; do
		umount -f $i > /dev/null 2>&1
	done
	umount /media/cdrom 2>/dev/null
	eject /dev/yaolinux-cd 2>/dev/null
	dialog --title "    $(gettext "Reboot the PC")    " \
	--msgbox "  $(gettext "Press OK to reboot the computer")  " 0 0 
	/sbin/reboot -f
}
setup_advanced_initrd() {
	if [ -d "$EFIVARS" ]; then
		dialog --title "   $(gettext "Welcome to the YaoLinuX installer")   " \
		--colors --menu "    $(gettext "Advanced Menu")   "  0 0 0 \
		"$(gettext "Help")" "$(gettext "How to do it") ? " \
		"$(gettext "Keyboard")" "$(gettext "Configure the keyboard") ($(gettext "optional"))" \
		"$(gettext "Partitioning")" "$(gettext "Create all your partitions") ($(gettext "optional"))" \
		"$(gettext "Format")" "$(gettext "Format a partition") ($(gettext "optional"))" \
		"$(gettext "Install")" "$(gettext "Install YaoLinuX")" \
		"$(gettext "Clock")" "$(gettext "Date and time settings") ($(gettext "optional"))" \
		"$(gettext "Console")" "$(gettext "Open a console")" \
		"$(gettext "Erase")" "$(gettext "Erase") YaoLinuX" \
		"$(gettext "Restart")" "$(gettext "Reboot the PC")" \
		"$(gettext "Main")" "$(gettext "Main Menu")" \
		2>$TMP/choix
	else
		dialog --title "   $(gettext "Welcome to the YaoLinuX installer")   " \
		--colors --menu "    $(gettext "Main Menu")   "  0 0 0 \
		"$(gettext "Help")" "$(gettext "How to do it") ? " \
		"$(gettext "Keyboard")" "$(gettext "Configure the keyboard") ($(gettext "optional"))" \
		"$(gettext "Partitioning")" "$(gettext "Create all your partitions") ($(gettext "optional"))" \
		"$(gettext "Format")" "$(gettext "Format a partition") ($(gettext "optional"))" \
		"$(gettext "Boot Bios")" "$(gettext "Configure the boot of the PC") ($(gettext "optional")) ($(gettext "skilled"))" \
		"$(gettext "Install")" "$(gettext "Install YaoLinuX")" \
		"$(gettext "Clock")" "$(gettext "Date and time settings") ($(gettext "optional"))" \
		"$(gettext "Console")" "$(gettext "Open a console")" \
		"$(gettext "Erase")" "$(gettext "Erase") YaoLinuX" \
		"$(gettext "Restart")" "$(gettext "Reboot the PC")" \
		"$(gettext "Main")" "$(gettext "Main Menu")" \
		2>$TMP/choix
	fi
	retval=$?
	choice=$(cat $TMP/choix)
	case $retval in
	0|2)
		case $choice in
			"$(gettext "Main")")
				setup_initrd
				;;
			"$(gettext "Help")")
				if [ -d "$EFIVARS" ]; then
					setup_help_efi
				else
					setup_help
				fi
				;;
			"$(gettext "Install")")
				echo "CHECK_SINGLE_PARTITION=$CHECK_SINGLE_PARTITION" > /dev/tty2
				[ "$CHECK_SINGLE_PARTITION" == "no" ] && unset PARTITION
				setup_install
				;;
			"$(gettext "Partitioning")")
				setup_partition
				;;
			"$(gettext "Format")")
				echo "CHECK_SINGLE_PARTITION=$CHECK_SINGLE_PARTITION" > /dev/tty2
				[ "$CHECK_SINGLE_PARTITION" == "no" ] && unset PARTITION
				setup_format
				;;
			"$(gettext "Boot Bios")")
				echo "CHECK_SINGLE_PARTITION=$CHECK_SINGLE_PARTITION" > /dev/tty2
				[ "$CHECK_SINGLE_PARTITION" == "no" ] && unset PARTITION
				setup_grub
				;;
			"$(gettext "Keyboard")")
				setup_keymap
				;;
			"$(gettext "Network")")
				setup_net
				;;
			"$(gettext "Clock")")
				setup_clock
				;;
			"$(gettext "Console")")
				source /etc/profile
				/usr/sbin/pwconv
				/usr/sbin/grpconv
				sh
				;;
			"$(gettext "Erase")")
				echo "CHECK_SINGLE_PARTITION=$CHECK_SINGLE_PARTITION" > /dev/tty2
				[ "$CHECK_SINGLE_PARTITION" == "no" ] && unset PARTITION
				setup_erase
				;;
			"$(gettext "Restart")")
				setup_reboot
				;;
		esac
		;;
	1)
		echo "1"
		;;
	255)
		echo "255"
		;;
	esac


}
setup_initrd() {
	dialog --title "   $(gettext "Welcome to the YaoLinuX installer")   " \
	--colors --menu "    $(gettext "Main Menu")   "  0 0 0 \
	"$(gettext "Install")" "$(gettext "Install YaoLinuX")" \
	"$(gettext "Advanced")" "$(gettext "Advanced Menu")" \
	2>$TMP/choix
	retval=$?
	choice=`cat $TMP/choix`
	case $retval in
	0|2)
		case $choice in
			"$(gettext "Advanced")")
				setup_advanced_initrd
				;;
			"$(gettext "Help")")
				if [ -d "$EFIVARS" ]; then
					setup_help_efi
				else
					setup_help
				fi
				;;
			"$(gettext "Install")")
				echo "CHECK_SINGLE_PARTITION=$CHECK_SINGLE_PARTITION" > /dev/tty2
				[ "$CHECK_SINGLE_PARTITION" == "no" ] && unset PARTITION
				setup_install
				;;
		esac
		;;
	1)
		echo "1"
		;;
	255)
		echo "255"
		;;
	esac

}
check_esp() {
	local FORMAT FIRST_DISK
	unset EFI_SYS_PART
	FIRST_DISK=`for i in $DISK_LIST;do echo $i;break;done`
	for VAR in `blkid $FIRST_DISK -o export`
	do
		local $VAR
	done
	if [ "$PTTYPE" != "gpt" ]; then
		dialog --title "   Hmmmm   " \
		--msgbox "    $(gettext "GPT partition table not found")" 6 60
	else
		EFI_SYS_PART="`(LANGUAGE=C fdisk ${FIRST_DISK} -l -o device,type|grep EFI|cut -d " " -f1)`"

		if [ -z "${EFI_SYS_PART}" ];then
			dialog --title "   Hmmmm   " \
			--msgbox "    $(gettext "EFI partition not found")" 6 60
		fi
	fi
}
setup_esp() {
	local FORMAT
	check_esp
	if [ ! -z $EFI_SYS_PART ]; then
		FORMAT="$(print_file_system $EFI_SYS_PART)"
		if [ "$FORMAT" != "vfat" ]; then
			dialog --title " $(gettext "Launch the format process ?") " \
			--colors --yesno "$(gettext "The partition") \Zb\Z1${EFI_SYS_PART}\Zn \
$(gettext "will be formatted in") \Zb\Z1vfat\Zn.\n\n\
$(gettext "Are you sure you want to continue ?")" 7 60
			retval2=$?
			case $retval2 in
				0)
					/usr/bin/mkfs.vfat $EFI_SYS_PART
					$SETUP_LOG "setup-nutyx" "EFI partition $EFI_SYS_PART formatted in vfat"
					;;
				1|255)
					break;;
			esac
		fi
		FORMAT="$(print_file_system $EFI_SYS_PART)"
		if [ "$FORMAT" == "vfat" ]; then
			$SETUP_LOG "setup-nutyx" "EFI partition $EFI_SYS_PART added in /etc/fstab"
			echo "$EFI_SYS_PART /boot/efi     vfat       defaults            0     0" >> /etc/fstab
		fi
	fi
}
check_swap() {
	unset SWAP_LIST
	SWAP_LIST="$(LANGUAGE=C fdisk -l -o device,type|grep swap|cut -d ' ' -f1)"
	if [ -z "${SWAP_LIST}" ];then
		dialog --title "   Hmmmm   " \
	--msgbox "    $(gettext "Swap partition not found")" 6 60
	fi
}
setup_swap() {
for i in ${SWAP_LIST}
do
	$SETUP_LOG "setup-nutyx" "swap partition $i added in /etc/fstab"
	echo "$i swap     swap       pri=1             0     0" >> /etc/fstab
	[ $(print_file_system $i) == "swap" ] || mkswap $i 2>1 > /dev/null
done
}
config_all() {
	echo "config the system"
}
print_help() {
	echo "usage: `basename $COMMAND` [options]"
	echo "options"
	echo ""
	echo "   start,                 $(gettext "Start the service")"
	echo "   -ok,   --check-all     $(gettext "Checking the configuration, returning true or false")"
	echo "   -sn,   --show-net      $(gettext "Show the network configuration")"
	echo "   -sk,   --show-keymap   $(gettext "Show the keyboard configuration")"
	echo "   -sc,   --show-clock    $(gettext "Show the date and time settings")"
	echo "   -sl,   --show-locale   $(gettext "Show the timezone and time adjustment")"
	echo "   -sa,   --show-all      $(gettext "Show all configurations")"
	echo "   -cl,   --config-lang   $(gettext "Configure the language")"
	echo "   -ul,   --update-lang   $(gettext "Configure the language")"
	echo "   -ck,   --config-keymap $(gettext "Configure the keyboard")"
	echo "   -cn,   --config-net    $(gettext "Configure the network")"
	echo "   -cw,   --config-wireless $(gettext "Configure the wireless access point")"
	echo "   -cc,   --config-clock  $(gettext "Date and time settings")"
	echo "   -cu,   --create-user   $(gettext "Add a user to the system")"
	echo "   -ca,   --config-all    $(gettext "Configure the system")"
	echo "    -h,   --help          $(gettext "Show the help")"
	echo "    -v,   --version       $(gettext "Show the version")"
}
initrd_only() {
	read -r cmdline < /proc/cmdline
	for param in $cmdline ; do
		case $param in
			root=*)
				root=${param#root=};;
		esac
	done
	if [ "$root" != "" ]; then
		print_help
		exit 1
	else
		# we are in the initrd
		SETUP_INITRD="yes"
		SETUP_START_SERVICE="yes"
	fi
}
parse_options() {
	if [ $# -lt 1 ]; then
		initrd_only
	else
		while [ "$1" ]; do
			case "${1}" in
				start)
					SETUP_START_SERVICE="yes"
					;;
				stop)
					kill
					;;
				-ok|--check-all)
					SETUP_CHECKALL="yes"
					;;
				-sn|--show-net)
					echo "show net"
					;;
				-sk|--show-keymap)
					echo "show keyboard"
					;;
				-sl|--show-locale)
					echo "show locale"
					;;
				-sc|--show-config)
					echo "show config"
					;;
				-sa|--show-all)
					echo "show all"
					;;
				-cl|--config-lang)
					check_root
					setup_language
					;;
				-ul|--update-lang)
					[ -f $LOCALE ] || exit 0
					source $LOCALE
					_msgfmt ${LANG%.*}
					exit 0
					;;
				-ck|--config-keymap)
					check_root
					unset KEYMAP
					setup_keymap
					;;
				-cn|--config-net)
					check_root
					setup_net
					;;
				-cw|--config-wireless)
					check_root
					setup_wireless
					;;
				-cc|--config-clock)
					check_root
					setup_clock
					;;
				-cu|--create-user)
					check_root
					setup_user
					;;
				-ca|--config-all)
					echo "config all"
					;;
				-v|--version)
					echo "`basename ${COMMAND}` $VERSION"
					exit 0
					;;
				-h|--help)
					print_help
					exit 0
					;;
				*)
					echo "`basename ${COMMAND}`: invalid option $1"
					exit 1
					;;
			esac
			shift
		done
	fi
}
check_all () {
	local RV
	RV=0
	if [ "$SETUP_CONFIG_LANGUAGE" = "no" ]; then
		if [ -r ${LOCALE} ]; then
			. ${LOCALE}
		else
			SETUP_CONFIG_LANGUAGE="yes"
		fi
		[ -r ${LOCALE} ] || SETUP_CONFIG_LANGUAGE="yes"
	fi
	if [ ! -r ${CONSOLE} ];then
		SETUP_CONFIG_KEYBOARD="yes"
		RV=$((RV+1))
	fi
	if [ -z ${NETWORK} ];then
		SETUP_CONFIG_NETWORK="yes"
		RV=$((RV+2))
	fi
	if [ ! -r ${CLOCK} ]; then
		SETUP_CONFIG_CLOCK="yes"
		RV=$((RV+4))
	fi
	if [ ! -d ${USER} ]; then
		SETUP_CONFIG_USER="yes"
		RV=$((RV+8))
	fi
	if [ ! -z "$WIFI_NETWORK_CARD" ];then
		if [ ! -f /etc/wpa_supplicant.conf.$WIFI_NETWORK_CARD ]; then
			SETUP_CONFIG_WIFI="yes"
			RV=$((RV+16))
		fi
	fi
	return $RV
}
cleanup () {
	if [ ! -z $TMP ]; then
		[ -d $TMP ] &&  rm -r $TMP
	fi
}

top_loop() {
	if [ "$SETUP_INITRD" = "yes" ]; then
		[ "$SETUP_CONFIG_LANGUAGE" = "yes" ] && LANG=fr_FR.utf8 setup_language
		if [ ! -d /media/cdrom/boot ]; then
			dialog --title "   $(gettext "Welcome to the YaoLinuX installer")   " \
				--msgbox "    $(gettext "You can remove the ISO media now.")" 6 60
		fi
		if [ -d "$EFIVARS" ]; then
			setup_help_efi
		else
			setup_help
		fi
		while [ "$SETUP_INSTALL" = "no" ];do
				[ "$SETUP_CONFIG_LANGUAGE" = "yes" ] && LANG=fr_FR.utf8 setup_language
				[ "$SETUP_INSTALL" = "no" ] && setup_initrd
		done
	else
		while [ "$SETUP_CONFIG_LANGUAGE" = "yes" ] ||
			[ "$SETUP_CONFIG_KEYBOARD" = "yes" ] ||
			[ "$SETUP_CONFIG_NETWORK" = "yes" ] ||
			[ "$SETUP_CONFIG_WIFI" = "yes" ] ||
			[ "$SETUP_CONFIG_CLOCK" = "yes" ] ||
			[ "$SETUP_CONFIG_USER" = "yes" ]; do
				[ "$SETUP_CONFIG_LANGUAGE" = "yes" ] && LANG=fr_FR.utf8 setup_language
				[ "$SETUP_CONFIG_KEYBOARD" = "yes" ] && setup_keymap
				[ "$SETUP_CONFIG_NETWORK" = "yes" ] && setup_net
				[ "$SETUP_CONFIG_WIFI" = "yes" ] && setup_wireless
				[ "$SETUP_CONFIG_CLOCK" = "yes" ] && setup_clock
				[ "$SETUP_CONFIG_USER" = "yes" ] && setup_user
		done
	fi

}
check_root () {
	if [ $UID -ne 0 ]; then
		echo 1>&2 "$(gettext "Please run this command as root")"
		exit 1
	fi
}
main () {
	parse_options "$@"
	TMP=$(mktemp -d)
	if [ "$SETUP_INITRD" == "yes" ]; then
		SETUP_CONFIG_LANGUAGE="yes"
		SETUP_INSTALL="no"
	fi

	if [ "$SETUP_CHECKALL" == "yes" ]; then
		check_all
		exit $?
	fi
	if [ "$SETUP_START_SERVICE" == "yes" ]; then
		log_info_msg "Checking configuration..."
		check_all
		if [ $? -ne 0 ]; then
			top_loop
		fi
		evaluate_retval
	fi
	cleanup
}

#
# VARIABLES
readonly COMMAND="$0"
readonly SETUP_ARCH="$(uname -m)"
readonly VERSION="#VERSION#"

DIALOG_OK=0
DIALOG_CANCEL=1
DIALOG_HELP=2

WPA_TOOL="$(which wpa_passphrase)"
IWCONFIG="$(which iwconfig)"

if [ ! -z "$IWCONFIG" ];then
	WIFI_NETWORK_CARD="`$IWCONFIG 2>&1|grep IEEE|cut -d " " -f1|tail -1`"
fi

SETUP_LOG="setup-nutyx-log"
CARDS="/etc/cards.conf"
XORGKB="/etc/X11/xorg.conf.d/20-keyboard.conf"
WAYLKB="/etc/profile.d/keyboard.sh"
CONSOLE="/etc/vconsole.conf"
LOCALE="/etc/locale.conf"
PROFILELOCALE="/etc/profile.d/i18n.sh"
LOCALTIME="/etc/localtime"
ZONEINFO="/usr/share/zoneinfo/"
NETWORK="$(ls /etc/systemd/network/* 2>/dev/null|head -1)"
CLOCK="/etc/adjtime"
USER="/home"
EFIVARS="/sys/firmware/efi/efivars"

SETUP_INITRD="no"
SETUP_INSTALL="no"
SETUP_CONFIG_LANGUAGE="no"
SETUP_CONFIG_KEYBOARD="no"
SETUP_CONFIG_NETWORK="no"
SETUP_CONFIG_CLOCK="no"
SETUP_CONFIG_USER="no"
SETUP_CONFIG_WIFI="no"

CHECK_HD_OK="no"
CHECK_PART_OK="no"
CHECK_SINGLE_PARTITION="no"

SETUP_LXDM_CONFFILE="/etc/lxdm/lxdm.conf"

main "$@"

# End of file
# vim:set ts=2 :
